Jump to content

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.

Edited by joecooper
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
}
Edited by Ch0cu3r

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".

 

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.