php将数据库查询的数据转换为json格式,为什么返回的格式前面有0 1 2 3

<?php header("Content-Type:text/html; charset=utf-8"); $con = mysql_connect('localhost','root','root');if(!$con){ die('error:'.mysql_error());}mysql_select_db("qq",$con); function my_json_sql($my_sql) {$arr = array();$result = mysql_query($my_sql);if (!mysql_num_rows($result)) $arr[]=0;//记录集没有记录情况else { while($row = mysql_fetch_array($result)){ $arr[] = $row; }}return $arr;}$q = "select src,price,name from la"; $result=my_json_sql($q);echo json_encode($result); ?>

稍微改一下你的自定义函数

function my_json_sql($my_sql){
$result = mysql_query($my_sql);

//判断执行结果
if($result && mysql_affected_rows()>0){
//先声明一个空数组
$arr=array();
//循环处理结果
while($row = @mysql_fetch_assoc($result)){
$arr[]=$row;
}
return $arr;
}
return false;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-03
mysql_fetch_array($result) 改成mysql_fetch_assoc($result)

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网