Call member function fetch_assoc () in boolean in <path>
I get the above error when running the code below to display orders made from the 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 just started getting an error message when I started posting it live. It works fine on my personal computer, and the database connection also works great.
The query method may return false instead of a result set if an error occurs. This is why you get an error when calling the fetch_assoc method, which obviously does not exist when $ result is false .
This means that you indicated an error in the SELECT statement. To display this error, do the following:
$result = $conn->query($sql) or die($conn->error); Most likely you have a misspelling table name or column name. Perhaps, when switching to the host, you did not create this table correctly and made a spelling mistake there.
In fact, you should see the same error when executing the same request through phpAdmin.
Also replace this line:
while(($row = $result->fetch_assoc()) !== null){ using only:
while($row = $result->fetch_assoc()) { You can also add this for debugging:
echo "number of rows: " . $result->num_rows; This error usually occurs when the table in the query does not exist. Just check the spelling of the table in the query and it will work.
see the following code. Highlight your code this way.
$query="select * from articles"; $result=$conn->query($query); $row = $result->fetch_assoc(); if($row) { echo $row['content']; } echo "no results founded"; ?> Ok, I just fixed this error.
This happens when there is an error in the query or the table does not exist.
Try debugging your purchase request by running it directly on phpmyadmin to confirm that the MySQL request is correct.
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(); } You need to update the php.ini configuration file on the server of the host provider, trust me, moreover, there is nothing wrong with your code. It took me almost a month and a half to understand that most hosting servers are not updated in php.ini files, for example. php 5.5 or later, I believe.