php中mysql_fetch_assoc ,mysql_fetch_array,mysql_fetch_row 三者的区别

使用mysqli_fetch_assoc()和mysqli_fetch_row()都是把查询结果返回到一个数组中,都是返回第一行然后指针下移一行。


1: mysqli_fetch_assoc()用关键字索引取值。比如: 

while($rows=mysql_fetch_assoc($result)){
    $data[]=$rows;
}

echo  json_encode($data);

返回结果:

[{"id":"1","name":"\u5f20\u4e09","mobile":"13031269256","city":"\u4e0a\u6d77","type":"\u79fb\u52a8","date":"2017-6-8"},{"id":"2","name":"\u674e\u56db","mobile":"19263586745","city":"\u5317\u4eac","type":"\u79fb\u52a8","date":"2017-5-8"},{"id":"3","name":"\u738b\u4e94","mobile":"17593586296","city":"\u5e7f\u5dde","type":"\u8054\u901a","date":"2017-6-9"},{"id":"4","name":"\u67d0\u67d0","mobile":"15932865956","city":"\u4e0a\u6d77","type":"\u8054\u901a","date":"2017-5-9"},{"id":"5","name":"\u9ec4\u5df2","mobile":"15963896256","city":"\u5e7f\u5dde","type":"\u79fb\u52a8","date":"2017-12-9"},


2:mysqli_fetch_row()用数字索引取值。比如: 

while($rows=mysql_fetch_row($result)){
    $data[]=$rows;
}

echo  json_encode($data);

返回结果:

[{"0":"1","id":"1","1":"\u5f20\u4e09","name":"\u5f20\u4e09","2":"13031269256","mobile":"13031269256","3":"\u4e0a\u6d77","city":"\u4e0a\u6d77","4":"\u79fb\u52a8","type":"\u79fb\u52a8","5":"2017-6-8","date":"2017-6-8"},{"0":"2","id":"2","1":"\u674e\u56db","name":"\u674e\u56db","2":"19263586745","mobile":"19263586745","3":"\u5317\u4eac","city":"\u5317\u4eac","4":"\u79fb\u52a8","type":"\u79fb\u52a8","5":"2017-5-8","date":"2017-5-8"},{"0":"3","id":"3","1":"\u738b\u4e94","name":"\u738b\u4e94","2":"17593586296","mobile":"17593586296","3":"\u5e7f\u5dde","city":"\u5e7f\u5dde","4":"\u8054\u901a","type":"\u8054\u901a","5":"2017-6-9","date":"2017-6-9"},{"0":"4","id":"4","1":"\u67d0\u67d0","name":"\u67d0\u67d0","2":"15932865956","mobile":"15932865956","3":"\u4e0a\u6d77","city":"\u4e0a\u6d77","4":"\u8054\u901a","type":"\u8054\u901a","5":"2017-5-9","date":"2017-5-9"},{"0":"5","id":"5","1":"\u9ec4\u5df2","name":"\u9ec4\u5df2","2":"15963896256","mobile":"15963896256","3":"\u5e7f\u5dde","city":"\u5e7f\u5dde","4":"\u79fb\u52a8","type":"\u79fb\u52a8","5":"2017-12-9","date":"2017-12-9"},


3:mysqli_fetch_array()既可以得到关联数组也可以得到索引数组,二者都有。比如: 

while($rows=mysql_fetch_array($result)){
    $data[]=$rows;
}

echo  json_encode($data);

返回结果:

[["1","\u5f20\u4e09","13031269256","\u4e0a\u6d77","\u79fb\u52a8","2017-6-8"],["2","\u674e\u56db","19263586745","\u5317\u4eac","\u79fb\u52a8","2017-5-8"],["3","\u738b\u4e94","17593586296","\u5e7f\u5dde","\u8054\u901a","2017-6-9"],["4","\u67d0\u67d0","15932865956","\u4e0a\u6d77","\u8054\u901a","2017-5-9"],["5","\u9ec4\u5df2","15963896256","\u5e7f\u5dde","\u79fb\u52a8","2017-12-9"]

猜你喜欢

转载自blog.csdn.net/hjw453321854/article/details/78909837