Here is the code I am running. The strange thing is if I run the exact same query in PHPMyAdmin it runs perfectly.
But for any ID over 19 the die()
clause activates but mysql_error()
is empty and the number of rows returned are 0.
This is the strangest thing I have ever seen as if I drop the id to 1 through 19 it works.
$sql = "SELECT * FROM booking WHERE `id`='21'";
$res = mysql_query($sql) or die(mysql_error());
if($res)
{
$ct = mysql_num_rows($res) or die(mysql_error());
echo $ct;
}
It looks and sounds like your "error" is on this line:
$ct = mysql_num_rows($res) or die(mysql_error());
With this, if mysql_num_rows($res)
returns 0
(meaning "no results found matching your query"), your or die(mysql_error())
portion will execute and, since there was no actual error, mysql_error()
will return empty.
Try removing the die()
part and change to something more user-friendly:
if($res) {
$ct = mysql_num_rows($res);
if ($ct === false) {
// actual error
die(mysql_error());
} else if ($ct === 0) {
// no results
echo 'No results =[';
} else {
echo $ct;
}
}
One of the two things is at work here.
$sql = "SELECT * FROM booking WHERE `id`='21'";
$res = mysql_query($sql) or die(mysql_error());
Not the case because the query is elementary and well constructed (albeit very unsafe, check PDO and parametrized queries).
$ct = mysql_num_rows($res) or die(mysql_error());
Which is what happens in your scenario because your query doesn't return any results - Double check that there is something to find above id 19.
On a related note, it's bad practice to die() if no results are found. Better to write a descriptive and informative message to the end user so that he can know what happened.