joecooper Posted October 23, 2013 Share Posted October 23, 2013 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 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)){ } Link to comment https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/#findComment-1455098 Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 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 } Link to comment https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/#findComment-1455099 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. Link to comment https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/#findComment-1455101 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) Link to comment https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/#findComment-1455104 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". Link to comment https://forums.phpfreaks.com/topic/283211-php-hangs-when-getting-num-rows/#findComment-1455111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.