php显示所有mysql记录,只显示一行

时间:2016-07-22 10:51:08

标签: php mysql

php显示所有mysql记录,只显示一行。请有人帮我这个吗?

PHP代码:

require_once('dbConnect.php');

$sql = "SELECT vendorID,joint_name,vendor_contact,vendor_name,vendor_place,joint_lat,joint_long,vendor_profile_pic,time_in,time_out,days_in,date FROM vendor ORDER BY vendorID desc ";

$r = mysqli_query($con,$sql);

$res = mysqli_fetch_array($r);

$result = array();

array_push($result,array(
                "joint_name"=>$res['joint_name'],
                "vendor_contact"=>$res['vendor_contact'],
                "vendor_name"=>$res['vendor_name'],
                "vendor_place"=>$res['vendor_place'],
                "joint_lat"=>$res['joint_lat'],
                "joint_long"=>$res['joint_long'],
                "vendor_profile_pic"=>$res['vendor_profile_pic'],
                "time_in"=>$res['time_in'],
                "Ttime_out"=>$res['time_out'],
                "days_in"=>$res['days_in'],
                "date"=>$res['date']                

         )
         );

echo json_encode(array("result"=>$result));
mysqli_close($con);

"
RESULT:
"
{"result":[{"joint_name":null,"vendor_contact":null,"vendor_name":null,"vendor_place":null,"joint_lat":null,"joint_long":null,"vendor_profile_pic":null,"time_in":null,"Ttime_out":null,"days_in":null,"date":null}]}

3 个答案:

答案 0 :(得分:0)

试试这个

while($res = mysqli_fetch_array($r)){
    array_push($result,array(
            "joint_name"=>$res['joint_name'],
            "vendor_contact"=>$res['vendor_contact'],
            "vendor_name"=>$res['vendor_name'],
            "vendor_place"=>$res['vendor_place'],
            "joint_lat"=>$res['joint_lat'],
            "joint_long"=>$res['joint_long'],
            "vendor_profile_pic"=>$res['vendor_profile_pic'],
            "time_in"=>$res['time_in'],
            "Ttime_out"=>$res['time_out'],
            "days_in"=>$res['days_in'],
            "date"=>$res['date']                

     )
     );
}

答案 1 :(得分:0)

那是因为你只执行了一次 mysqli_fetch_array 功能。将它放在while循环中以获取与结果集一样多的行。

您可以将其放入while循环以获取表格中的所有行。

像这样,

while($res = mysqli_fetch_array($r)){
      array_push($result,array(
                "joint_name"=>$res['joint_name'],
                "vendor_contact"=>$res['vendor_contact'],
                "vendor_name"=>$res['vendor_name'],
                "vendor_place"=>$res['vendor_place'],
                "joint_lat"=>$res['joint_lat'],
                "joint_long"=>$res['joint_long'],
                "vendor_profile_pic"=>$res['vendor_profile_pic'],
                "time_in"=>$res['time_in'],
                "Ttime_out"=>$res['time_out'],
                "days_in"=>$res['days_in'],
                "date"=>$res['date']                

         )
         );
}

$result将包含result-set中的所有行。

  

mysqli_fetch_array()返回一个与获取的行对应的数组,如果有,则返回NULL   不再是结果表示的结果集的行   参数。

因此,只要没有更多行从结果集中获取,while循环就会停止工作。

答案 2 :(得分:0)

您可以使用mysqli_fetch_all在数组中获取所有结果。您也不需要手动复制结果。然后,您的代码将简化为:

require_once('dbConnect.php');
$sql = "SELECT vendorID,joint_name,vendor_contact,vendor_name,vendor_place,joint_lat,joint_long,vendor_profile_pic,time_in,time_out,days_in,date FROM vendor ORDER BY vendorID desc ";
$r = mysqli_query($con,$sql);
echo json_encode("result" => mysqli_fetch_all($r));

当然,你应该做错误检查。

作为旁注,您不应让dbConnect.php定义全局变量$con。更好地提供工厂功能。