Freid001 Posted July 30, 2010 Share Posted July 30, 2010 Hi I am trying to select and order data/numbers from a colum in a mysql data base however i run the code and it returns no value just a blank page no errors or any thing so i think the code is working right but then it returns no result? Please help thanks Here is the code: <?php $host= "XXXXXX"; $mysql_user = "XXXXXX"; $mysql_password = "XXXXXX"; $mysql_database = "XXXXXXX"; $connection = mysql_connect("$host","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server."); mysql_select_db($mysql_database) or die ("Unable to select requested database."); $row = mysql_fetch_assoc( mysql_query( "SELECT XP FROM Game ORDER BY number DESC LIMIT 1" ) ); $number = mysql_result(mysql_query("SELECT XP FROM Game ORDER BY number DESC LIMIT 1"), 0); echo "The the highest XP is $number"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/ Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Share Posted July 30, 2010 Not solving the main problem here, but your echo should be echo "The highest XP is " . $number; instead of echo "The the highest XP is $number"; Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093100 Share on other sites More sharing options...
Adam Posted July 30, 2010 Share Posted July 30, 2010 echo "The the highest XP is $number"; Actually that's perfectly valid. however i run the code and it returns no value just a blank page no errors or any thing so i think the code is working right but then it returns no result? Do you see the "The the highest XP is" text? You may have errors disabled. Add at the top of your code: ini_set('display_errors', 1); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093102 Share on other sites More sharing options...
aleX_hill Posted July 30, 2010 Share Posted July 30, 2010 Try this? $query = "SELECT XP FROM Game ORDER BY number DESC LIMIT 1"; $result - mysql_query($query); $row = mysql_fetch_assoc($result); echo "The the highest XP is". $row['XP']; Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093108 Share on other sites More sharing options...
Andy17 Posted July 30, 2010 Share Posted July 30, 2010 Ah damn, I guess you have to escape the $ if you want to show "$number" then (pretty unique syntax). Sorry about that; I just always did it the other way. Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093109 Share on other sites More sharing options...
Freid001 Posted July 30, 2010 Author Share Posted July 30, 2010 Thanks this has given me a error now. However im not sure how to fix it? error shown: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /hosted/subs/ulmb.com/e/u/europeanwar/public_html/test/leader.php on line 15 code: <?php ini_set('display_errors', 1);error_reporting(E_ALL); $host= "xxxxx"; $mysql_user = "xxxxxx"; $mysql_password = "xxxxxx"; $mysql_database = "xxxxx"; $connection = mysql_connect("$host","$mysql_user","$mysql_password") or die ("Unable to connect to MySQL server."); mysql_select_db($mysql_database) or die ("Unable to select requested database."); $query = "SELECT XP FROM Game ORDER BY number DESC LIMIT 1"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "The the highest XP is". $row['XP']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093150 Share on other sites More sharing options...
aleX_hill Posted July 30, 2010 Share Posted July 30, 2010 Assuming that you fixed my error where i had a dash instead of an equal sign for $result = mysql_query... then there is probably a syntax error with your query. To troubleshoot I would replace "XP" in the query with * and see what happens. Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093156 Share on other sites More sharing options...
Freid001 Posted July 30, 2010 Author Share Posted July 30, 2010 I still get this error message: :'( Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /hosted/subs/ulmb.com/e/u/europeanwar/public_html/test/leader.php on line 14 Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093185 Share on other sites More sharing options...
aleX_hill Posted July 30, 2010 Share Posted July 30, 2010 Basically what is happening is that when you run the query you should get a resource stored in $result, but if there is an error int the query (which there is in your case) then it stores false in $result, which is why the error is telling you it has been given a boolean. OK, so are you sure that your table is called Game (with a capital). Are you sure that you can sort by number? Try $query = "SELECT * FROM Game"; first, then slowly add in other components (ie ORDER and LIMIT) to see what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093187 Share on other sites More sharing options...
Freid001 Posted July 30, 2010 Author Share Posted July 30, 2010 OK This works it gives me the xp for the person at the top of the data base but no further down?: $query = "SELECT XP FROM Game"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "The the highest XP is". $row['XP']; But when i add ORDER BY number: $query = "SELECT XP FROM Game ORDER BY number"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); echo "The the highest XP is". $row['XP']; this happens : Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /hosted/subs/ulmb.com/e/u/europeanwar/public_html/test/leader.php on line 14 The the highest XP is My sql Data base feild: The fild name is XP it is set to text , Collation Latin1_swedish_ci and Null is notnull :'( :'( :'( Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093196 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 What is the data type of the field named `number`? Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093198 Share on other sites More sharing options...
Freid001 Posted July 30, 2010 Author Share Posted July 30, 2010 Not sure what you mean there is no field called number. I think that number is suposed to just take data from XP and order it. Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093214 Share on other sites More sharing options...
Pikachu2000 Posted July 30, 2010 Share Posted July 30, 2010 That's kind of what I thought was happening. ORDER BY orders the results based on the contents of a field, therefore you have to specify a field name. $query = "SELECT XP FROM Game ORDER BY XP"; Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093218 Share on other sites More sharing options...
Freid001 Posted July 30, 2010 Author Share Posted July 30, 2010 Thanks so much it works now !!! :D :D ;) Quote Link to comment https://forums.phpfreaks.com/topic/209342-selecting-data-and-ordering-it-from-a-mysql-data-base/#findComment-1093234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.