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

标签 php mysql

我正在尝试为我的 Android 应用程序获取 json,但出现错误: mysqli_fetch_array() 期望参数 1 为 mysqli_result,该对象在 /home/a5872453/public_html/GetAllGreenhouse.php17 行

中给出

这是我的代码:

  <?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);


?>

最佳答案

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

<?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);

关于从服务器检索多行时出现php错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32465282/

相关文章:

mysql - 从表 A 中选择 *,但在第 1 列中显示它与表 B 中的匹配项

php - PHP foreach 循环在重叠日期时间上出现问题

php - Mysql - php 上传数据 - 错误 - 1366 不正确的字符串值

php - MySQL查询不适用于PHP变量

一旦我添加 'echo' 并从表中获取一些内容,PHP (MySQL) 脚本就不会读取任何其他内容

php - 使用 volley 将 BLOB 图像从 mysql 显示到 android

php - 从数据库中删除选定的行

php - 在mysql上用foreach显示数据数组

mysql - 在 SQL 语句搜索中查找 ruby​​ 哈希值

mysql - 按行合并两个mysql表