I am trying to retrieve mysql data
in json
format using php, below is my code, but after execution I am getting blank output, if I check the output of the array elements using var_dump()
then I can see the records but as a whole the json
output is not showing, as i am new to json
so not able to troubleshoot, can anyone check and find the problem, it will be great thnx.
<?php
header('Content-Type: application/json');
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'mypassword';
$dbname = 'mydbname';
//setting records limit per page is 15
$rec_limit = 15;
//Establishing Connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Get total number of records */
$sql = "SELECT count(*) FROM ImageUploads";
$retval = $conn->query($sql);
if(! $retval )
{
die($mysqli->error.__LINE__);
}
$rec_count = $retval->fetch_row();
$rec_count = $rec_count[0];
// Checking for page parameter to set.
if( isset($_GET{'page'} ) )
{
$page = $_GET{'page'} + 1;
$offset = $rec_limit * $page ;
}
else
{
$page = 0;
$offset = 0;
}
//getting all data from table
$sql = "SELECT * FROM ImageUploads ORDER BY slno DESC ".
"LIMIT $offset, $rec_limit";
$retval = $conn->query($sql);
if(! $retval )
{
die($mysqli->error.__LINE__);
}
//creating an array for response
$response = array();
if ($retval->num_rows > 0) {
$response["wallpapers"] = array();
$response["success"] = 1;
$response["count"]= $rec_count;
while ($row = $retval->fetch_array()) {
// temp wallpaper array
$wallpaper = array();
$wallpaper["id"] = $row["slno"];
$wallpaper["orig_url"] = "http://doupnow.com/AndroidApp/ImageUploads/".$row["image_url"];
$wallpaper["thumb_url"]="No Thumb";
$wallpaper["downloads"] = $row["downloads"];
$wallpaper["fav"] = $row["views"];
// push all data into final response array
array_push($response["wallpapers"], $wallpaper);
}
// echoing JSON response
echo str_replace('\/','/',json_encode($response,JSON_PRETTY_PRINT));
} else {
// no wallpapers found
$response["success"] = 0;
$response["message"] = "No Wallpapers found";
}
mysqli_close($conn);
?>
output of var_dump($response) is --
array(3) {
["wallpapers"]=>
array(1) {
[0]=>
array(5) {
["id"]=>
string(1) "1"
["orig_url"]=>
string(95) "http://doupnow.com/AndroidApp/ImageUploads/raman.das@gmail.com_sOw$vN8T_IMG_20171203_200638.jpg"
["thumb_url"]=>
string(8) "No Thumb"
["downloads"]=>
string(1) "0"
["fav"]=>
string(1) "0"
}
}
["success"]=>
int(1)
["count"]=>
string(1) "1"
}