从mysql读取数据时没有用于php代码的json输出

时间:2017-12-14 07:10:11

标签: php mysql json

我正在尝试使用php以mysql data格式检索json,下面是我的代码,但是在执行后我得到空白输出,如果我使用{{1检查数组元素的输出然后我可以看到记录,但整体上var_dump()输出没有显示,因为我是json的新手,因此无法排除故障,任何人都可以检查并发现问题,它会是伟大的thnx。

json

var_dump($ response)的输出是 -

<?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);
?>

2 个答案:

答案 0 :(得分:1)

问题解决了,只是一个简单的问题,感谢上帝。我将此行更改为 -

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

现在我得到了json输出。

答案 1 :(得分:0)

好像你的问题似乎有问题 $ wallpaper [“orig_url”] =“http://doupnow.com/AndroidApp/ImageUploads/”。$ row [“image_url”]; 从中删除$ row [“image_url”]并测试它。