Rik Nijdeken Rik Nijdeken - 4 months ago 9
PHP Question

Fatal error: Call to a member function fetch_all() on boolean

I am trying to make al the records in my database to a JSON format so i can use it in an app from me, but then i got this error

<b>Fatal error</b>: Call to a member function fetch_all() on boolean
i have tried changing
fetch_all to fetchall
but it did not help. this is my code that i use:

<html>
<body>
<?php
//Converting db values into json data

header('Content-type:application/json');

$conn = new mysqli('localhost','root','') or die(mysql_error());
$sql= 'SELECT * FROM wtd';
$rs=$conn->query($sql);

$data = $rs->fetch_all(MYSQLI_ASSOC);
echo json_encode($data);


?>
</body>
</html>


I hope anyone can help me i am on a time limit.
Thanks!

Answer

This is because you have not selected any database. You can set the database like this:

$conn = new mysql('localhost', 'root', '', '<NAME_OF_DATABASE>');

Since you have not selected the database $conn->query() will return FALSE.

Comments