I'm having a strange issue with the MySQL I'm using on my site. Some days, in place of my code, I will get:
Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in [the current file] on line 14
The code is the following:
<?php
include("../../configsql/configsemester.php");
$query="SELECT * FROM duedates_f2011 GROUP BY actualdate";
$result=mysql_query($query);
$numrows=mysql_numrows($result);
$i=0;
while($i < $numrows)
{
$actualdate=mysql_result($result,$i,"actualdate");
$duedate=mysql_result($result,$i,"duedate");
$styleclass="date"; mysql_result($result,$i,"styleclass");
$today=date("Y-m-d"); //H:i:s");
$query2="SELECT * FROM duedates_f2011 WHERE duedate = \"" . $duedate . "\"";
$result2=mysql_query($query2);
$numrows2=mysql_numrows($result2);
$k=0;
if( $today > $actualdate)
{ $styleclass="done duedate"; }
echo "<div class=\"" . $styleclass . "\">
\t";
echo "<h3>" . $duedate . "</h3>
";
echo "<ul>
\t";
while($k < $numrows2)
{
$assignmentname=mysql_result($result2,$k,"assignmentname");
$duetime=mysql_result($result2,$k,"duetime");
$coursecode=mysql_result($result2,$k,"coursecode");
$link=mysql_result($result2,$k,"link");
//echo "<div class=\"" . $styleclass . "\">
\t";
if(is_null($link)) { echo "<li>" . $coursecode . ": " . $assignmentname . " @ " . $duetime . "</li>"; } else { echo "<li><a href=\"" . $link . "\">" . $coursecode . ": " . $assignmentname . " @ " . $duetime . "</a></li>"; }
//echo "<li>" . $coursecode . ": " . $assignmentname . " @ " . $duetime . "</li>";
$k++;
}
echo "
</ul>
";
$i++;
if($i != $numrows)
{
echo "<hr class=\"nonmobile\" />
"; //so that it is not displayed at the end, also this must be inside the <div> so that it is included
}
echo "</div>
";
}
mysql_close();
?>
It should be noted that the code above is part of a page which is include()'d inside another page, which also use MySQL. I have made sure to open/close the database at the appropriate spots in this exterior code, though, and again, it usually works.
I'm just wondering what would be causing these errors to come about, only sometimes.
if you put
or die(mysql_error())
before the ; on line 14
you will get the full explanation the next time it crashes
You call the function mysql_numrows() but it is actually called mysql_num_rows().
EDIT
It seems that PHP also accepts mysql_numrows(), so that is not the problem.
The actual error indicates that there was an error executing the query. As suggested in other answers, you should use mysql_error() to find out what the actual error is.
Your query is not very complicated, is it possible that the table duedates_f2011 does not exist in the database? Or that it does not have an attribute called actualdate? I'm just guessing now :)
If there are no records returned this can happen, or if the server can't be connected to, etc. I would add this to the end of all your mysql_query() lines to figure out the issue, especially with more than one query on the page:
mysql_query($query) or die(mysql_error()); // so it will stop execution and output any errors on whatever line is the issue.