Jump to content

Name retrieved from mysql_query won't show up in echo


Lt. Geyser

Recommended Posts

Ok, so I have this page that accepts input from a user in the form of a form. The name of this input is "player." It is an integer value.

 

I want to compare this integer value to a table in a mySQL database (the table's name is "Player") and get the lName (this is short for last name) value in the table that is associated with this integer value (I.e. it is in the same row). Then I want to print out a short message telling the user the 'lName' value of the 'player' value they had chosen.

 

This is how I tried to do it:

 

<?
$player=$_POST['player'];    //the user input from the previous page is set to $player

mysql_connect("localhost", "xxxx", "xxxx") or die(mysql_error());
mysql_select_db("xxxx") or die(mysql_error());                            //connecting to the database

$player_name = mysql_query("SELECT lName FROM Player WHERE playerID = $player");  
//setting the value 'player_name' equal to the 'lName' associated with the 'player' value (I think this is where the problem is) 

echo "Thank you for choosing $player_name"; //Print a message telling the user the 'lName' of who they had chosen

mysql_close();
?>

 

 

When I try to do this, however, the echo doesn't print the $player_name. It's just blank. I've been googling and agonizing for hours. Any ideas?

It should be this:

 

$player_name_result = mysql_query("SELECT lName FROM Player WHERE playerID = $player") or die(mysql_error());
if (mysql_num_rows($player_name_result) == 1) {
  $player_name = mysql_result($player_name_result, 0, 0);
}

 

There are many other ways to get the data out of a mysql result.

It's likely your query returns no results then.

 

Try changing it to this:

 

$player_name_result = mysql_query("SELECT lName FROM Player WHERE playerID = $player") or die(mysql_error());
if (mysql_num_rows($player_name_result) == 1) {
  $player_name = mysql_result($player_name_result, 0, 0);
} else {
  print "Sorry, the player id $player could not be found in the database<br>";
}

The query should return a value. I believe the problem is in getting the database to recognize the $player variable.

 

Looking around a bit more, could it be possible that the problem is something like on this page?:

 

http://www.php.net/function.mysql-query

 

In example 1376, instead of using the variables themselves, they replace them with %s and then use something called mysql_real_escape_string() to reference them. But it only seems to work with strings. What would I use for integers? ('player' is an integer, remember)

 

 

There's no need to use that.  What you have is fine.

 

Can you add this to your script:

 

print "Player = $player<br>";

 

That ensures that $player is set to what you expect it to be.  If $player is not set, can you post the form?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.