achilles1971 Posted October 31, 2011 Share Posted October 31, 2011 In the following code, at the mysql query immediately following the <!--end accordianButton div-->, Some of the rows will echo out the content, while some of the rows will throw the Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in... error. Any idea why? Go here to see what I mean: http://www.chalmerscommunitychurch.com/P2P_archives.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pastor to People Blog Archives</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"> </script> <script type="text/javascript" src="js/javascript.js"> </script> <style> .accordionButton { width: 100%; height:30px; float: left; background: url(../images/button.png); border-bottom: 1px solid #FFFFFF; cursor: pointer; } .accordionContent { width: 100%; float: left; display: none; } </style> </head> <body> <?php require("include.php"); $con = mysql_connect("$db_host","$db_username","$db_pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("chalmers_db", $con); $result = mysql_query("SELECT title, date FROM blog"); while($row = mysql_fetch_array($result)) { $title=$row['title']; $sqldate=$row['date']; $date=date('m-d-Y',strtotime($sqldate)); ?> <!--start accordionButton div--> <div class="accordionButton"><?php echo $title;?>, <?php echo $date;?> </div> <!--end accordianButton div--> <?php $query = mysql_query("SELECT content FROM blog WHERE title = '".$title."' ORDER BY date DESC"); while ($row = mysql_fetch_array($query)){ $content = $row['content']; ?> <!--start accordionContent div--> <div class="accordionContent" align="justify"> <?php echo $content;?> </div> <!--end accordionContent div--> <?php } ?> <?php } ?> <p><p><p><p><a href="http://www.chalmerscommunitychurch.com"><h3>Back to Chalmers Community Church</h3></a> </body> </html> Quote Link to comment Share on other sites More sharing options...
joel24 Posted October 31, 2011 Share Posted October 31, 2011 if it's working only sometimes, then there must be no blog entries for some... check to see if results returned with mysql_num_rows() $query = mysql_query("SELECT content FROM blog WHERE title = '".$title."' ORDER BY date DESC"); if (mysql_num_rows($query) { #now do the fetch while ($row = mysql_fetch_array($query)){ #etc etc } Quote Link to comment Share on other sites More sharing options...
trq Posted October 31, 2011 Share Posted October 31, 2011 Before checking anything with [mysql_num_rows[/m] you should check your query actually succeeded. The general syntax is: if ($result = mysql_query($sql)) { if (mysql_num_rows($result)) { // $result contains data, use it } else { // no records found } } else { // query failed } Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 31, 2011 Share Posted October 31, 2011 if it's working only sometimes, then there must be no blog entries for some... check to see if results returned with mysql_num_rows() Although thorpe didn't state it explicitly, he was trying to allude to the fact that the reason for the error is that the query is failing - not that there were no results. Simply doing a mysql_num_rows() on a failed query will generate the same type of error. That error would not occur if the query succeeded with 0 results. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2011 Share Posted October 31, 2011 I'm going to guess that some of the titles in $title probably contain some sql special characters that need to be escaped before being put into the inner query. Faux Edit: There's actually no need for two queries. You can use one to accomplish the same thing. Just detect when the title changes to close out a previous section and output a new heading. Real Edit2: Visiting the link in the 1st post in the thread confirms that there are ' in the titles that fail. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted October 31, 2011 Share Posted October 31, 2011 I see your inserting into the database without escaping your strings. I have a feeling the rows that are failing have a quote in them and the ones that work don't have a quote in them. This could be the problem... Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 31, 2011 Share Posted October 31, 2011 Your php code using one query - <?php require("include.php"); $con = mysql_connect("$db_host","$db_username","$db_pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("chalmers_db", $con); $heading = null; // remember the last heading $result = mysql_query("SELECT title, date, content FROM blog ORDER BY date DESC"); while($row = mysql_fetch_array($result)){ if($heading != $row['title']){ // a new heading detected, start a new section here... $heading = $row['title']; // save the new title $date=date('m-d-Y',strtotime($row['date'])); ?> <!--start accordionButton div--> <div class="accordionButton"><?php echo "{$row['title']}, $date";?> </div> <!--end accordianButton div--> <?php } // handle each piece of data here... ?> <!--start accordionContent div--> <div class="accordionContent" align="justify"><?php echo $row['content'];?> </div> <!--end accordionContent div--> <?php } ?> Quote Link to comment 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.