3raser Posted February 21, 2011 Share Posted February 21, 2011 My code: $extract = mysql_query("SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"); while ($row = mysql_fetch_assoc($extract)) { echo "<div class='right'><h2>".$row['title']."</h2><div class='articles'>".nl2br(stripslashes($row['message']))."</div></div>"; } Error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/stonedms/public_html/justin/Toplist/index.php on line 29 I've double checked my database, and it seems like I've have everything correct. Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/ Share on other sites More sharing options...
cssfreakie Posted February 21, 2011 Share Posted February 21, 2011 can you do <?php print_r ($extract); //and paste what you get? //place at bottom of code below the query ?> Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177468 Share on other sites More sharing options...
3raser Posted February 21, 2011 Author Share Posted February 21, 2011 can you do <?php print_r ($extract); //and paste what you get? //place at bottom of code below the query ?> Tried that and it doesn't seem to print out anything. <?php $extract = mysql_query("SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"); while ($row = mysql_fetch_assoc($extract)) { echo "<div class='right'><h2>".$row['title']."</h2><div class='articles'>".nl2br(stripslashes($row['message']))."</div></div>"; } print_r ($extract); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177470 Share on other sites More sharing options...
kenrbnsn Posted February 21, 2011 Share Posted February 21, 2011 Replace <?php $extract = mysql_query("SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"); ?> with <?php $q = "SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"; $rs = mysql_query($q) or die("problem with the query: $q<br>" . mysql_error()); $extract = mysql_query($rs); ?> And see if you get an error printed. Ken Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177471 Share on other sites More sharing options...
cssfreakie Posted February 21, 2011 Share Posted February 21, 2011 seems $extract is empty and certainbly not an array. you can double check it by doing echo $extract; //it should Print Array() Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177472 Share on other sites More sharing options...
3raser Posted February 21, 2011 Author Share Posted February 21, 2011 Replace <?php $extract = mysql_query("SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"); ?> with <?php $q = "SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1"; $rs = mysql_query($q) or die("problem with the query: $q<br>" . mysql_error()); $extract = mysql_query($rs); ?> And see if you get an error printed. Ken problem with the query: SELECT id,title,message FROM blog ORDER BY id DESC LIMIT 1 No database selected Edit: I do have the correct mysql connect information. Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177474 Share on other sites More sharing options...
kenrbnsn Posted February 21, 2011 Share Posted February 21, 2011 The error says "No Database Selected". Please show the code where you select the DB (remove any connection details like username/password). Ken Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177478 Share on other sites More sharing options...
3raser Posted February 21, 2011 Author Share Posted February 21, 2011 The error says "No Database Selected". Please show the code where you select the DB (remove any connection details like username/password). Ken <?php session_start(); $title = "MCTop"; $description = "The site to find your popular SMP needs!"; $mysql_host = "localhost"; $mysql_database = ""; $mysql_user = ""; $mysql_password = ""; mysql_connect("$mysql_host", "$mysql_user", "$mysql_password"); mysql_select_db("$mysql_database"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177481 Share on other sites More sharing options...
BlueSkyIS Posted February 21, 2011 Share Posted February 21, 2011 there is no need to put double-quotes around lone variables. i would also check for errors after executing mysql commands... mysql_connect($mysql_host, $mysql_user, $mysql_password) or die(mysql_error()); mysql_select_db($mysql_database) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1177607 Share on other sites More sharing options...
3raser Posted February 26, 2011 Author Share Posted February 26, 2011 I did the above, yet it still didn't work. The query seems to be hard headed. Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1180065 Share on other sites More sharing options...
jcbones Posted February 26, 2011 Share Posted February 26, 2011 What error did mysql_select_db() return? BTW $extract = mysql_query($rs); //should be: $extract = mysql_fetch_assoc($rs); //because mysql_query returns a resource result, and the fetch functions return array's Quote Link to comment https://forums.phpfreaks.com/topic/228356-mysql-error/#findComment-1180157 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.