Jump to content

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result reso


achilles1971

Recommended Posts

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>

Link to comment
Share on other sites

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
} 

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.