AlanB09 Posted December 14, 2009 Share Posted December 14, 2009 hi there, im running the following code to access my database and print out results but keep getting the message: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/a3866796/public_html/databasetest.php on line 16 ive looked through and cant see any errors in my coding.. any suggestions please ? *i have edited out password/username/server detail for privacy. * many thanks <?php $username="private"; $password="private"; $database="private"; $server="private"; $db_handle= mysql_connect($server, $username, $password); $db_found = mysql_select_db($database, $db_handle); if($db_found) { $SQL="SELCT*FROM tb_a3866796_users"; $result = mysql_query($SQL); while ($db_field = mysql_fetch_assoc($result) ) { print $db_field['ID']."<br>"; print $db_field['username']."<br>"; print $db_field['password']."<br>"; print $db_field['datejoined']."<br>"; } mysql_close($db_handle); } else { print "Database not Found!"; mysql_close($db_handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/ Share on other sites More sharing options...
Brian W Posted December 14, 2009 Share Posted December 14, 2009 Please use code tags... if you don't know what they are, read the forum rules before posting. But, I did read your problem and despite the lack of code tags will give you a nudge in the correct direction. That error message means that the query had an error, hence making the result un-usable to mysql_fetch_assoc(). Try changing your query execution line to this: $result = mysql_query($SQL) or die(mysql_error()); (btw, the above used code tags) what that does is say "if mysql_query() returns FALSE because of an error, kill the application and output the error message". Note: mysql_query() will return TRUE if it does not get any results, it will only return FALSE if there is an error in the query. Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/#findComment-977256 Share on other sites More sharing options...
AlanB09 Posted December 14, 2009 Author Share Posted December 14, 2009 okay apologies for my ignorance :$ and thanks , ill give it a go should the while ($db_field = mysql_fetch_assoc($result) ) { remain the same then? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/#findComment-977260 Share on other sites More sharing options...
AlanB09 Posted December 14, 2009 Author Share Posted December 14, 2009 **edit** problem solved... check syntax of table name ** okay to add to this yup changing the $result code got rid of that error, but now ive got something else lol ! ive looked it up , and tried what i can to sort it.. changing the name and things, but i keep getting this error Table 'a3866796_users.tb_userlists' doesn't exist im basically trying to access the table userlists and get a print of the output. pulling my hair out over here ! Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/#findComment-977279 Share on other sites More sharing options...
Brian W Posted December 14, 2009 Share Posted December 14, 2009 that means that you have the wrong table name. Check and be sure that in your query you specify the correct name of the table. Also, your user data is going to print vertically, but you can deal with that once you've got your query fixed. Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/#findComment-977291 Share on other sites More sharing options...
ignace Posted December 14, 2009 Share Posted December 14, 2009 SELECT * FROM tb_a3866796_users @Brian W that should be: $result = mysql_query($SQL) or trigger_error(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/185126-basic-sql-troubles/#findComment-977303 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.