Lt. Geyser Posted April 13, 2007 Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/ Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228111 Share on other sites More sharing options...
Lt. Geyser Posted April 13, 2007 Author Share Posted April 13, 2007 That seems have the same problem. It still only prints out "Thank you for choosing". Link to comment https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228133 Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228142 Share on other sites More sharing options...
Lt. Geyser Posted April 13, 2007 Author Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228156 Share on other sites More sharing options...
btherl Posted April 13, 2007 Share Posted April 13, 2007 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 https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228161 Share on other sites More sharing options...
Lt. Geyser Posted April 13, 2007 Author Share Posted April 13, 2007 Oh wait, it just started working somehow...could have sworn it wasn't before. Guess I screwed up the syntax. Anyway, way to go dude. That totally fixed it. Thank you. Link to comment https://forums.phpfreaks.com/topic/46803-name-retrieved-from-mysql_query-wont-show-up-in-echo/#findComment-228199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.