Error message: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result ...
Results 1 to 4 of 4

Thread: Error message: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result ...

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    2

    Error message: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result ...

    I have only a little experience with PHP having mostly used straight HTML and CGI/Perl.

    I have encountered the following error message when running a query and cannot resolve it, which is a little perplexing!

    Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in [absolute path to file] on line 120

    Line 120 of file=

    Code:
    $numrows = mysqli_num_rows($result);
    The REALLY puzzling thing is that when I run the page and query on my localhost computer (PHP 5.4.7; MySQL 5.5.27) it runs fine and returns a list, just as it should. It's only when the files are transferred to and run on the remote server (PHP 5.2 or 5.4, MySQL 5.0 - 1and1.com) that it fails to run (I have changed all the database accesses and passwords!).

    I'm sure it's something simple and stupid, but I'm at a loss! Assistance would be appreciated!!

    Code is below.

    Thank you!!

    PHP Code:
        <?php
        error_reporting
    (E_ALL);
        
    ini_set'display_errors','1'); 

        if (isset(
    $_POST['submitted'])) {

        
    // connect to database
        
    include('../connect.php');

        
    $category $_POST['category'];
        
    $criteria $_POST['criteria'];
        
    $query "SELECT * FROM erstallions WHERE $category LIKE '%".$criteria."%'";
        
    $result mysqli_query($dbcon$query);
        
    $numrows mysqli_num_rows($result);


     
    // If the query has results ...
     
    if ($numrows 0)
     {
         
    // ... print out a header
         
    if ($criteria == '')
         {
         print 
    "<br /><center><font size=\"+2\"><b>Stallion Listing</b></font></center><br>";
         }
         else
         {
         print 
    "<center><font size=\"+2\"><b>Information about the stallion '$criteria'<br>(Frozen by <i>Equine-Reproduction.com LLC</i>)</b></font></center><br>";
         };

         
    // and start a <table>.
         
    print "\n<table width=\"100%\" border=\"1\" align=\"center\">\n<tr>" .
               
    "\n\t<th>Stallion Name</th>" .
               
    "\n\t<th>Owner</th>" .
               
    "\n\t<th>Breed</th>" .
               
    "\n\t<th>Reg. Number</th>" .
               
    "\n\t<th>Birth Date/Year</th>\n</tr>";

         
    // Fetch each of the query rows
         
    while ($row mysqli_fetch_array($resultMYSQLI_ASSOC)) 
         {
            
    // Print one row of results
              
    print "\n<tr>\n\t<td><a href=\"StallionDetails.php?StallionName={$row["StallionName"]}\" target=\"_blank\">{$row["StallionName"]}</a></td>" .
              
    "\n\t<td>{$row["OwnerName"]}</td>" .
              
    "\n\t<td>{$row["StallionBreed"]}</td>" .
              
    "\n\t<td>{$row["StallionRegNo"]}</td>" .
              
    "\n\t<td><center>{$row["DoB"]}</center></td>\n</tr>";
         } 
    // end while loop body

         // Finish the <table>
         
    print "\n</table>";
     } 
    // end if $rowsFound body

     // Report how many rows were found
              
    print "<center>{$numrows} record(s) found matching your criteria</center><br>";


        } 
    //End of main if statement

        
    ?>

  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    2
    I should add that I do have an error check in the "commercial presentation" but removed it to enable me to get all the error messages!

    Code:
    $result = mysqli_query($dbcon, $query) or die('Stallion Listings are temporarily unavailable - Please check back later!');

  3. #3
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,429
    You never check to see if mysqli_query() returned boolean FALSE - indicating an error has occurred - and, if so, outputting and/or logging the SQL error message for debugging.

    EDIT: Removing the 'or die()' is good - you should never use this construct anyway (it's a pretty poor way of handling errors for both you and your end user). However, you still need to add code that checks what the 'or die()' clause was essentially checking before.

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,887
    Quote Originally Posted by bradgrafelman View Post
    EDIT: Removing the 'or die()' is good - you should never use this construct anyway (it's a pretty poor way of handling errors for both you and your end user). However, you still need to add code that checks what the 'or die()' clause was essentially checking before.
    Rather than just calling die(), you might want to configure your code so that you can show an error page. DO NOT show the actual error to the user. Write it to a log file and show the user "an error has been encountered" or something and send a 5XX HTTP code using header (or 404 or something).

    BG, perhaps we should put up a sticky about MySQL best practices? A couple of things KEEP coming back:
    * DO NOT USE MYSQL_* FUNCTIONS
    * CHECK THE RESULTS OF YOUR QUERY
    * ESCAPE USER INPUT BEFORE STICKING IT IN A QUERY

    etc.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •