Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中从MySQL准备的语句生成JSON?_Php_Mysql_Json - Fatal编程技术网

如何在PHP中从MySQL准备的语句生成JSON?

如何在PHP中从MySQL准备的语句生成JSON?,php,mysql,json,Php,Mysql,Json,我用PHP编写了这段代码,我试图根据准备好的语句的结果生成JSON。问题是它返回的是一个完全白色的页面,没有显示任何内容 $con = mysqli_connect(HOST,USER,PASS,DB); $batch_sql = "select * from batchrecord"; $batch_res = mysqli_query($con,$batch_sql); $row = mysqli_fetch_array($batch_res); $batch_num = $row[0];

我用PHP编写了这段代码,我试图根据准备好的语句的结果生成JSON。问题是它返回的是一个完全白色的页面,没有显示任何内容

$con = mysqli_connect(HOST,USER,PASS,DB);

$batch_sql = "select * from batchrecord";
$batch_res = mysqli_query($con,$batch_sql);
$row = mysqli_fetch_array($batch_res);
$batch_num = $row[0];
$start = $batch_num * 100;
$end = $start + 99;


if ($stmt = mysqli_prepare($con, "select tweetid, body from tweet where id >= 
? and id <= ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "ii", $start, $end);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $tweetid, $body);

    $result = array();

    /* fetch value */
    while(mysqli_stmt_fetch($stmt)){
        array_push($result,
        array('Id'=>$tweetid,
        'Body'=>$body,
        ));
    }

    /* close statement */
    mysqli_stmt_close($stmt);

    echo json_encode(array("result"=>$result), JSON_UNESCAPED_UNICODE);
}
else{
echo "Statement Prepare Error";
}

mysqli_close($con);
$con=mysqli\u connect(主机、用户、密码、数据库);
$batch\u sql=“从batchrecord中选择*”;
$batch\u res=mysqli\u查询($con,$batch\u sql);
$row=mysqli\u fetch\u数组($batch\u res);
$batch_num=$row[0];
$start=$batch_num*100;
$end=$start+99;
如果($stmt=mysqli\u prepare($con),则从tweet中选择tweetid,body from tweet where id>=
?和id像这样尝试

 $result = array();
 while(mysqli_stmt_fetch($stmt)){
       $result[] = array('Id'=>$tweetid,'Body'=>$body);
    }

演示:

我在一些调试后发现了问题。问题出在json_enconde函数中,由于
json_ERROR\u UTF8
类型的错误,该函数一直在默默地失败。在成功使用
json_UNESCAPED\u UNICODE
之前,我的母语中存在一些特殊字符的问题,但这次我添加了
mysqli\u set\u charset($con,“utf8”);
到代码顶部,它现在正在工作。为完整起见,语言为巴西葡萄牙语。完整代码如下

$con = mysqli_connect(HOST,USER,PASS,DB);

mysqli_set_charset($con, "utf8");

$batch_sql = "select * from batchrecord";
$batch_res = mysqli_query($con,$batch_sql);
$row = mysqli_fetch_array($batch_res);
$batch_num = $row[0];
$start = $batch_num * 100;
$end = $start + 99;


if ($stmt = mysqli_prepare($con, "select tweetid, body from tweet where id >= 
? and id <= ?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "ii", $start, $end);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $tweetid, $body);

    $result = array();

    /* fetch value */
    while(mysqli_stmt_fetch($stmt)){
        $result[] = array('Id'=>$tweetid,'Body'=>$body);
    }

    /* close statement */
    mysqli_stmt_close($stmt);

    echo json_encode(array("result"=>$result), JSON_UNESCAPED_UNICODE);
 }
else{
echo "Statement Prepare Error";
}

mysqli_close($con);
$con=mysqli\u connect(主机、用户、密码、数据库);
mysqli_set_charset($con,“utf8”);
$batch\u sql=“从batchrecord中选择*”;
$batch\u res=mysqli\u查询($con,$batch\u sql);
$row=mysqli\u fetch\u数组($batch\u res);
$batch_num=$row[0];
$start=$batch_num*100;
$end=$start+99;
如果($stmt=mysqli\u prepare($con),则从tweet中选择tweetid,body from tweet where id>=

?和id注意:
mysqli
的面向对象接口明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的
mysql\u查询接口混淆。在您对过程风格投入过多之前,值得切换。例如:
$db=new mysqli(…)
$db->prepare(“…”)
过程接口是PHP4时代的产物,当时引入了
mysqli
API,不应该在新代码中使用。我不明白为什么您有两个查询我会尝试面向对象,不知何故,过程风格对我来说似乎更为熟悉。至于这两个查询,是的,我注意到我只有一个查询没有您没有这么复杂,我当时很匆忙,没有想太多。谢谢!请看我上面的答案。谢谢你的帮助,演示帮助了很多!