This php connection code is throwing an error...
This is the full code:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
$conn = new mysqli("localhost", "myusername", "mypassword", "mydatabase");
$result = $conn->query("SELECT title FROM mytable");
var_dump($result);
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
$outp .= '"title":"'. $rs["title"] . '"}';
}
$outp ='{"records":['.$outp.']}';
$conn->close();
echo($outp);
?>
This is the error that it's throwing:
Fatal error: Call to a member function fetch_array() on a non-object in /home/mypath/public_html/connection.php on line 13
The error points here:
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
How can I fix this?