20

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
dhool
  • 291
  • 1
  • 3
  • 14
  • Yeah tried that too and same issue. – dhool Feb 27 '16 at 11:28
  • I tried your code, its working for me, check your database and query ? – Niklesh Raut Feb 27 '16 at 11:29
  • What output you are getting after $result = $conn->query($sql); print_r($result); – Niklesh Raut Feb 27 '16 at 11:30
  • 1
    It's seems your query failed, and the query call returned a boolean FALSE. – Hardik Solanki Feb 27 '16 at 11:31
  • when I run that query $conn->query($sql); print_r($result); I get no output. When you say check your database, what do you mean to check? I mean im having other functional issues so it could well be the database but what could it be? All these functions im having issues with are working when locally hosted using Xampp – dhool Feb 27 '16 at 11:36
  • did you get and result after running query in phpmyadmin ? _SELECT id, tablename, numseats, person FROM confirms_ – Niklesh Raut Feb 27 '16 at 11:51
  • did you try by running sql query ? – Niklesh Raut Feb 27 '16 at 12:18
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Mar 04 '20 at 00:03

5 Answers5

40

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;
trincot
  • 317,000
  • 35
  • 244
  • 286
  • OK so that helped in that I was linking to the wrong database name, fixed that but still the same in that no output is being outputted. The "or die($conn->error)" now returns nothing but im confused as to why no entries are not being displayed. – dhool Feb 27 '16 at 12:48
  • What is the output when you run the SELECT directly in phpAdmin (make sure you are on the right server)? – trincot Feb 27 '16 at 12:52
  • 5
    **This means you have an error in your SELECT statement.** This saves my life. – Siraj Alam Apr 07 '18 at 11:09
  • 1
    "Most probably you have a wrong spelling for the table name or a column name." Thanks for that – Marco Floriano Dec 21 '19 at 14:51
5

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.

ASammour
  • 865
  • 9
  • 12
3

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

Shoaib
  • 67
  • 4
-2

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();
}
Andriy
  • 2,767
  • 2
  • 21
  • 29
  • I treid your suggestion and that now removes the error but no output is being outputted and their are definitely entries in the database to be outputted. – dhool Feb 27 '16 at 11:45
-3

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.