Solving PHP Warning mysqli_num_rows() expects, boolean given

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Solving PHP Warning mysqli_num_rows() expects, boolean given

    Well, here it is, usually problematic part of code is like:

    PHP Code:
    $query mysqli_query($conn'SELECT COUNT(`id`) FROM `table` WHERE `id` = '.$id.'');
        
    $count mysqli_num_rows($query); 
    ... which is throwing error:

    Code:
    PHP Warning:  mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /www/user/public_html/index.php on line 2
    ... so the query must be in if():

    PHP Code:
    if ($query mysqli_query($conn'SELECT COUNT(`id`) FROM `table` WHERE `id` = '.$id.'')) {
        
    $count mysqli_num_rows($query);

    But this is throwing this error:

    Code:
    PHP Notice:  Undefined variable: count in /www/user/public_html/index.php on line 2
    ... so simple define it:

    PHP Code:
    // if you want integer:
    $count = (int)''// perfectly valid way
    // if you want string:
    $count '';
    // or use settype(): http://php.net/manual/en/function.settype.php
    settype($count'int');
    if (
    $query mysqli_query($conn'SELECT COUNT(`id`) FROM `table` WHERE `id` = '.$id.'')) {
        
    $count mysqli_num_rows($query);

    And there will be no Warning nor Notice.
    Last edited by arnage; 15.01.13, 22:41.
    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS
Working...
X