从服务器检索多行时出现php错误

时间:2015-09-08 18:57:46

标签: php mysql

我试图为我的Android应用程序获取一个json,但我收到一个错误: mysqli_fetch_array()期望参数1为mysqli_result, /home/a5872453/public_html/GetAllGreenhouse.php 17

中给出的对象

这是我的代码:

  <?php

$con=mysqli_connect("mysql3.000webhost.com","xxx","xxx","xxx");

$farmname = $_POST["farmname"];


 $statement = mysqli_prepare($con, "SELECT * FROM GreenHouse WHERE farm_name = ?");
    mysqli_stmt_bind_param($statement, "s", $farmname);
    mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $ghid, $name, $farmname, $planttype, $linefrom, $lineto, $barcode);



$response["greenhouses"] = array();

while($row = mysqli_fetch_array($statement)){
        $greenhouse= array();
    $greenhouse["ghid"]=$row[$ghid];
    $greenhouse["name"]=$row[$name];
    $greenhouse["farmname"]=$row[$farmname];
    $greenhouse["planttype"]=$row[$planttype];
        $greenhouse["linefrom"]=$row[$linefrom];
        $greenhouse["lineto"]=$row[$lineto];
        $greenhouse["barcode"]=$row[$barcode];

        // push single product into final response array
        array_push($response["greenhouses"], $greenhouse);
}


echo json_encode($response);
mysqli_stmt_close($statement);
mysqli_close($con);


?>

1 个答案:

答案 0 :(得分:0)

好的...谢谢我解决了。这是正确的代码:

<?php

$con=mysqli_connect("mysql3.000webhost.com","xxx","xxx","xxx");
$farmname = $_POST["farmname"];


 $statement = mysqli_prepare($con, "SELECT * FROM GreenHouse WHERE farm_name= ?");
    mysqli_stmt_bind_param($statement, "s", $farmname);
    mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $ghid, $name, $farmname, $planttype, $linefrom, $lineto,$barcode);

$greenhouses["greenhouses"] = array();

while(mysqli_stmt_fetch($statement)){
     $greenhouse= array();
    $greenhouse["ghid"]=$ghid;
    $greenhouse["name"]=$name;
    $greenhouse["farmname"]=$farmname;
    $greenhouse["planttype"]=$planttype;
        $greenhouse["linefrom"]=$linefrom;
        $greenhouse["lineto"]=$lineto;
        $greenhouse["barcode"]=$barcode;
   array_push($greenhouses["greenhouses"], $greenhouse);

}

echo json_encode($greenhouses);
mysqli_stmt_close($statement);
mysqli_close($con);