1

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/[email protected]_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"
}
3
  • This maybe related stackoverflow.com/questions/19361282/… Commented Dec 14, 2017 at 7:14
  • Show what you are getting in $response and in var_dump also Commented Dec 14, 2017 at 7:19
  • @YashParekh............the output of var_dump($response) is attached. Commented Dec 14, 2017 at 7:25

2 Answers 2

1

Problem resolved, just a simple issue, thank god. i changed this line as --

echo str_replace('\/','/',json_encode($response));
//echo str_replace('\/','/',json_encode($response,JSON_PRETTY_PRINT));

Now i am getting the json output.

Sign up to request clarification or add additional context in comments.

1 Comment

After 2 days, you can accept your own answer, which will help others who read this question in future
0

seems like there is a problem in your $wallpaper["orig_url"] = "http://doupnow.com/AndroidApp/ImageUploads/".$row["image_url"]; remove the $row["image_url"]from this and test it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.