Re: PHP hangs with empty result set

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, 2011-03-23 at 15:34 -0400, Curtis Maurand wrote:
> 
> I've been having a problem when querying a database with php and the mysql
> library  the offending code follows.  If the result is an empty
> set, PHP hangs.  I've had to add code to the script to set up a max
> execution time to kill the script after 30 seconds if it doesn't
> complete.  This is happening on a very busy site and in the past, it
> has brought the server to its knees.  The code that follows is actual
> code copied and pasted from the script.  I've trie wrapping an
> "if ($result_2) {...}: and "if (mysql_num_rows($result_2) >
> 0) { ..}" around the while loop and it's made no difference.
> 
> thanks in advance, 
> Curtis
> 
> 
> $dbhandle2 = mysql_connect($hostname, $username, $password)
>  or
> die("Unable to connect to MySQL");
> //echo "Connected
> to MySQL<br>";
> 
> //select a database to work with
> $selected = mysql_select_db("database",$dbhandle2)
>   or
> die("Could not select examples");
> 
>         while
> ($_parent !="0") {
>                 $result_2 =
> mysql_query("SELECT catagory_parent FROM t_catagories where
> catagory_ID=" .$_parent);
>                 $num_rows_2 =
> mysql_num_rows($result_2);
>                 if($num_rows_2 >
> "0")
>                 {
>                         while
> ($row = mysql_fetch_array($result_2)) {
>                              
>   $_parent= $row{'catagory_parent'};
>                                
> $_parents[$counter] = $row{'catagory_parent'};
>                       
>          $counter++;
>                         }
>                
> }
>         }
>         mysql_close($dbhandle2);
> 

The only obvious thing that I can see is that you're checking if the
number of results is greater than a string, not a number. I believe PHP
automagically converts it into an integer for the comparison, but try
changing it to an actual integer and seeing if that resolves it.

There is one other time you use a zero in quotes like that, but without
seeing the rest of the code it's impossible to tell whether this would
be causing an issue. Check to see if you really do want to compare the
string value. Again, this should be automatically converted by PHP, but
there are cases where the conversion isn't what you might expect.

Lastly, I noticed you're using curly braces instead of square brackets
to reference the $row array values. Was this just a typo or your actual
code? Array elements really should be referenced by square brackets.

-- 
Thanks,
Ash
http://www.ashleysheridan.co.uk




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux