Php 给出空结果的JSON
我正在尝试从位于web服务器上的数据库中获取数据Php 给出空结果的JSON,php,json,Php,Json,我正在尝试从位于web服务器上的数据库中获取数据 <?php define('HOST','localhost.000webhostapp.com'); define('USER','id1206871_myuser'); define('PASS','******'); define('DB','id1206871_mydb'); $con = mysqli_connect(HOST,USER,PASS,DB); $sql = "select * from perso
<?php
define('HOST','localhost.000webhostapp.com');
define('USER','id1206871_myuser');
define('PASS','******');
define('DB','id1206871_mydb');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from person";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'address'=>$row[2]
));
}
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo json_encode(array("result"=>$result));
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
mysqli_close($con);
?>
我已经将我的PHP文件get_data.PHP上传到web服务器上
<?php
define('HOST','localhost.000webhostapp.com');
define('USER','id1206871_myuser');
define('PASS','******');
define('DB','id1206871_mydb');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from person";
$res = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'address'=>$row[2]
));
}
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo json_encode(array("result"=>$result));
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
mysqli_close($con);
?>
当我尝试这个URL时
我明白了
{“结果”:null}最后一个错误:未发生错误
我还尝试验证JSON
错误:第1行的分析错误:mrpcml.comli.com/g^
'字符串','数字','空','真','假','{','[',得到'未定义'
为什么我得到这个空值?问题在哪里?
我不确定我的db主机。你的$result变量是在while循环内部声明的(在外部执行)。另外,请确保你将其初始化为数组。var_dump()&echo是你的朋友。。我的朋友!!我是php新手。。我不知道我是否按照你说的做了。。我将代码更改为..$result=array();$res=mysqli_query($con,$sql);而($row=mysqli_fetch_array($res))的结果是:{“result”:[]}要将内容推送到循环内部的php数组中,需要这样的内容:$result[]=$row;