I have a table with multiple rows where I want to encode all the rows with json.
I have been looking for other question and solutions and I have tried lots of different approaches but json_encode still returns null
<?php
$mysqli = new mysqli('localhost', 'root', 'password', 'testBasParmak');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sth = mysql_query("SELECT * FROM pictures");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
$error = json_last_error();
print $error;
$mysqli->close();
?>
output in terminal is
[]0
if I try this
$sth = mysql_query("SELECT * FROM pictures");
$rows = array("id" => $id,"name" => $name,"description" => $description,"url" => $url,"users_id" => $users_id,"users_id" => $users_id,"totalvoteup" => $totalvoteup,"totalvotedown" => $totalvotedown,"totalvoteneutral" => $totalvoteneutral);
while($r = mysql_fetch_assoc($sth)) {
$row[] = $r;
}
print json_encode($rows);
$error = json_last_error();
print $error;
output in terminal is
{"id":null,"name":null,"description":null,"url":null,"users_id":null,"totalvoteup":null,"totalvotedown":null,"totalvoteneutral":null}0
Maybe it does not send correct query?