Jump to content

PHP hangs when getting num rows


joecooper

Recommended Posts

I have this same code elsewhere and it runs without a problem

 

Basically, the script wants to put some data in the database, but if the row already exisits, it needs to ignore it. I am using mysql_num_rows for this.

$sql="SELECT * FROM grid WHERE `id` = $nextcell LIMIT 1";
$result=mysql_query($sql);
$numrows = mysql_num_rows($result);
if ($numrows=0){  //nothing here, input data
    //code to insert
}

It hangs on the 2nd line.

 

I added "die("$nextcell");" to check it is returning a number, and it does.

Link to comment
https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/
Share on other sites

If the script stops on mysql_query() then there is a problem with the query. To see if there is an error use mysql_error() to get the error from mysql.

$sql="SELECT * FROM grid WHERE `id` = $nextcell LIMIT 1";
if($result=mysql_query($sql)) // check that query executed
{
   $numrows = mysql_num_rows();
   if ($numrows=0){  //nothing here, input data
       //code to insert
   }
}
// mysql_query returned false, probably due to error
else
{
   echo 'MySQL Error: ' . mysql_error(); // display error from msyql
}

Also note that you are using one "=" sign, so you are putting zero into $numrows, instead of checking if $numrows has zero in it.

 

if ($numrows=0){ //nothing here, input data

 

should be

 

if ($numrows==0){ //nothing here, input data

 
and to make prevent this bug in future; teach yourself to always put the constant first:
 

if (0==$numrows)

 

Then, if you forget an "=" sign you will get an error from PHP because you cannot change the value of the constant "0".

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.