2

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!

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Rik Nijdeken
  • 49
  • 1
  • 7

2 Answers2

0

You must select a database:

$conn = new mysqli('localhost','root','', '**database**') or die(mysql_error());
Emil
  • 1,786
  • 1
  • 17
  • 22
0

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.

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93