Check for data for that specific player

C++, C#, Java, PHP, ect...
Post Reply
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Check for data for that specific player

Post by vitinho444 »

Hey Indie resource community!

Im trying to make a simple game just to check some things and stuff..

Basicly all the info of a certain thing is on a table just for that

then each player has one/multiple rows depending the thing of course.

The structure is basicly

VARCHAR for Player Name (to indentify the player)
INT for Ammount (the thing ammount)
INT for Thing_ID (the thing id)

but if the player has 2 differents things it creates 2 rows for each one but for only that player!


Now i want to know whats the query to gather all the rows just for that player and display them...

I used:

Code: Select all

$result=mysql_fetch_array(mysql_query("SELECT * from negocios where player='$player'"));

$num=mysql_numrows($result);


$i=0;
while ($i < $num) {

$variable = mysql_result($result,$i,"variable");

$i++;

but it didnt work

This error appears: Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 23

line 23 is: $num=mysql_numrows($result);


Thanks for the help ;)
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Check for data for that specific player

Post by Chris »

You are trying to count an array with mysql_numrows. What you should be doing is counting what the query is retrieving:

Code: Select all

$query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'");
$result = mysql_fetch_array($query);
$num = mysql_numrows($result);
 
This will now count the amount of rows found where the field `player` = $player

Another solution to this would simply to do what you are doing and then count the amount of values $result contains:

Code: Select all

$result=mysql_fetch_array(mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'"));
$num=count($result);
 
The only problem with this is calling mysql_fetch_array only retrieves on row at a time. So calling it once will only retrieve one row, leaving you array with only one value. Meaning $num will always equal 1.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Check for data for that specific player

Post by vitinho444 »

same error :(


my code:

Code: Select all

$query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'");
$result = mysql_fetch_array($query);
$num = mysql_numrows($result);

Warning: mysql_numrows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 24
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Check for data for that specific player

Post by Chris »

Oops my bad:

Code: Select all

$num = mysql_numrows($query); // not $result
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Check for data for that specific player

Post by vitinho444 »

Well.. now i got the that error on another line:

Code: Select all

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 38

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 39

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 40

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 41

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 42

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 43

Warning: mysql_result() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\game\all.bus.php on line 44
Here are that lines:

Code: Select all

$negocio = mysql_result($result,$i,"negocio");
$lucro = mysql_result($result,$i,"lucro");
$gastos = mysql_result($result,$i,"gastos");
$numero = mysql_result($result,$i,"numero");
$nivel = mysql_result($result,$i,"nivel");
$alugado = mysql_result($result,$i,"alugado");
$comprado = mysql_result($result,$i,"comprado");

My code from the query:

Code: Select all

$info=mysql_fetch_array(mysql_query("SELECT * from players where username='$player'"));


$query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'");
$result = mysql_fetch_array($query);

$num = mysql_numrows($query); // not $result

$i=0;
while ($i < $num) {

$negocio = mysql_result($result,$i,"negocio");
$lucro = mysql_result($result,$i,"lucro");
$gastos = mysql_result($result,$i,"gastos");
$numero = mysql_result($result,$i,"numero");
$nivel = mysql_result($result,$i,"nivel");
$alugado = mysql_result($result,$i,"alugado");
$comprado = mysql_result($result,$i,"comprado");
$i++;

}
if($alugado)
{
echo "<b><br>Negocio: " . $negocio . " |  Lucro: " . $lucro . " |  Gastos: " . $gastos . "Nivel: " . $nivel . "Renda: " . $alugado . "<br></b>";
}
else
{
echo "<b><br>Negocio: " . $negocio . " |  Lucro: " . $lucro . " |  Gastos: " . $gastos . "Nivel: " . $nivel . "(Loja Comprada!)<br></b>";

}

echo "<br><h3><br><a href='index.php'>Voltar</a><br>";
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Check for data for that specific player

Post by Chris »

I think you need to use $query instead of $result.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Check for data for that specific player

Post by vitinho444 »

same error...

isnt there another way?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Check for data for that specific player

Post by Chris »

No. I'll try explain again.

What's happening is you are sending you database a query:

Code: Select all

$query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'"); 
This is then stored as a resource in $query. This resource can be used to read information from.

What you are then doing is retrieving information from $query:

Code: Select all

$reuslt = mysql_fethc_array($query); 
This is now storing data as an array in PHP. This is not a resource. $query is still the resource.


So if you are going to try and pass the first parameter in mysql_result() as an array when it should be a resource, it's going to throw an error:

Code: Select all

$negocio = mysql_result($result,$i,"negocio"); 
What you should be doing is using $query instead of $result:

Code: Select all

$negocio = mysql_result($query,$i,"negocio");
$lucro = mysql_result($query,$i,"lucro");
$gastos = mysql_result($query,$i,"gastos");
$numero = mysql_result($query,$i,"numero");
$nivel = mysql_result($query,$i,"nivel");
$alugado = mysql_result($query,$i,"alugado");
$comprado = mysql_result($query,$i,"comprado"); 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
vitinho444
Posts: 2819
Joined: Mon Mar 21, 2011 4:54 pm

Re: Check for data for that specific player

Post by vitinho444 »

Ok first.. Thank you for your time ;)

second:

I got the code in this way now:

Code: Select all

    $query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'"); 
	$result = mysql_fetch_array($query);
	
	$num = mysql_numrows($query);
echo "<h3>" . $player . " aqui podes gerir os teus negócios:</h3>";

	
$i=0;
while ($i < $num) {

    $negocio = mysql_result($query,$i,"negocio");
    $lucro = mysql_result($query,$i,"lucro");
    $gastos = mysql_result($query,$i,"gastos");
    $numero = mysql_result($query,$i,"numero");
    $nivel = mysql_result($query,$i,"nivel");
    $alugado = mysql_result($query,$i,"alugado");
    $comprado = mysql_result($query,$i,"comprado"); 

$i++;
}
Now the good notice is: There are no errors.

The bad new is: Nothing appears (it means nothing is fetched)

And yes i got at least 1 record inside the table for this player :/

It just shows the Title of the page and the Back link...


Thanks for the help but i dont see the problem here :(
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Check for data for that specific player

Post by Ark »

Try echoing the mysql_results the inside the while statement.

Code: Select all

<?php

  $query = mysql_query("SELECT * FROM `negocios` WHERE `player`='$player'"); 
   $result = mysql_fetch_array($query);
   
   $num = mysql_numrows($query);
echo "<h3>" . $player . " aqui podes gerir os teus negócios:</h3>";

   
$i=0;
while ($i < $num) {

    $negocio = mysql_result($query,$i,"negocio");
    $lucro = mysql_result($query,$i,"lucro");
    $gastos = mysql_result($query,$i,"gastos");
    $numero = mysql_result($query,$i,"numero");
    $nivel = mysql_result($query,$i,"nivel");
    $alugado = mysql_result($query,$i,"alugado");
    $comprado = mysql_result($query,$i,"comprado"); 
    
    echo $negocio."<br />";
    echo $lucro."<br />";
    echo $gastos."<br />";
    echo $numero."<br />";
    echo $nivel."<br />";
    echo $alugado."<br />";
    echo $comprado."<br />";

$i++;
}
?>
Orgullo Catracho
Post Reply

Return to “Coding”