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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.