Php 脚本在获取结果时返回null
我正在尝试使用PHP脚本查询数据库并传递JSON字符串。我正在使用jQueryAjax中的这个脚本Php 脚本在获取结果时返回null,php,Php,我正在尝试使用PHP脚本查询数据库并传递JSON字符串。我正在使用jQueryAjax中的这个脚本 <?php $con = mysqli_connect("localhost","root","password","test") or die("Some error occurred during connection " . mysqli_error($con)); $strSQL = "SELECT name,id from build";
<?php
$con = mysqli_connect("localhost","root","password","test") or die("Some error occurred during connection " . mysqli_error($con));
$strSQL = "SELECT name,id from build";
$query = mysqli_query($con, $strSQL);
$builder_json=array();
$row_array=array();
while($result = mysqli_fetch_array($query))
{
// $builder_json[]=$result['name'].":".$result['id'];
$row_array['name'] = $result['name'];
$row_array['id'] = $result['id'];
array_push($builder_json[],$row_array);
}
echo json_encode($builder_json);
mysqli_close($con);
?>
现在,如果获取的数据如下所示:
姓名-A,B
Id-1,2
我希望JSON字符串类似于-{“A”:“1”,“B”:“2”};
使用上面的代码,我得到NULL。我犯了什么错误
在数组的push调用中,变量后面不需要[]
//array_push($builder_json[],$row_array);
array_push($builder_json,$row_array);
否则会产生以下错误:
警告:array_push()要求参数1为array,第行给定为null从array_push中删除
[]
($builder_json[],$row_array);