[php] Call to a member function fetch_assoc() on boolean in <path>

I'm getting the above error when running the below code to display bookings made from a database.

<?php
        
        $servername = "localhost";
        $username = "*********";
        $password = "********";
        $dbname = "thelibr1_fyp";


        // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
        // Check connection
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
        
        $sql = "SELECT id, tablename, numseats, person FROM confirms";
        $result = $conn->query($sql);
        ?>
                        
        <table id="Confirms" border ="2" style="length:900px;width:350px;">
              <thead>
                <tr style= "background-color: #A4A4A4;">
                  <td>Booking ID:</td>
                  <td>Table No.:</td>
                  <td>No. of Seats:</td>
                  <td>Person:</td>
                </tr>
              </thead>
            <tbody>
                <?php
                  while(($row = $result->fetch_assoc()) !== null){
                    echo
                    "<tr>
                      <td>{$row['id']}</td>
                      <td>{$row['tablename']}</td>
                      <td>{$row['numseats']}</td>
                      <td>{$row['person']}</td>
                    </tr>\n";
                  }
                ?>
            </tbody>
        </table>

I only started to receive the error when i started hosting it live. It works fine on my personal computer, the databse connection works fine also.

This question is related to php mysqli

The answer is


This error happen usually when tables in the query doesn't exist. Just check the table's spelling in the query, and it will work.


You have to update the php.ini config file with in your host provider's server, trust me on this, more than likely there is nothing wrong with your code. It took me almost a month and a half to realize that most hosting servers are not up to date on php.ini files, eg. php 5.5 or later, I believe.


Please use if condition with while loop and try.

eg.

if ($result = $conn->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {

    }
    /* free result set */
    $result->free();
}

OK, i just fixed this error.

This happens when there is an error in query or table doesn't exist.

Try debugging the query buy running it directly on phpmyadmin to confirm the validity of the mysql Query


The query method can return false instead of a result set in case there is an error. That is why you get the error on the fetch_assoc method call, which obviously does not exist when $result is false.

This means you have an error in your SELECT statement. To get that error displayed, do this:

 $result = $conn->query($sql) or die($conn->error);

Most probably you have a wrong spelling for the table name or a column name. Maybe when moving to the host you did not create that table correctly, and made a spelling mistake there.

You should in fact see the same error when executing the same query via phpAdmin.

Also, replace this line:

while(($row = $result->fetch_assoc()) !== null){

with just:

while($row = $result->fetch_assoc()) {

You could also add this for debugging:

echo "number of rows: " . $result->num_rows;