joecooper Posted October 23, 2013 Share Posted October 23, 2013 (edited) 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 October 23, 2013 by joecooper Quote Link to comment Share on other sites More sharing options...
joecooper Posted October 23, 2013 Author Share Posted October 23, 2013 It also hangs when using: $sql="SELECT * FROM grid WHERE `id` = '$nextcell' LIMIT 1"; $result=mysql_query($sql); if (mysql_fetch_array($result)){ } Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) 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 October 23, 2013 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
joecooper Posted October 23, 2013 Author Share Posted October 23, 2013 I rewrote the query and now is working fine. Odd as this code used to work on a previous server. $result = mysql_query("SELECT * FROM grid WHERE id=$nextcell LIMIT 1"); if(mysql_fetch_array($result) == false){ } Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 23, 2013 Share Posted October 23, 2013 Also if ($numrows=0) should be if ($numrows==0) Quote Link to comment Share on other sites More sharing options...
vinny42 Posted October 23, 2013 Share Posted October 23, 2013 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". Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.