0

嗨,我正在检查 fetch 数组是否会给我 0 行数据,所以我使用 num_row() 但我总是给我一个错误

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\PostImages\displayImages.php on line 8

这是我使用的代码

<?php
$con = mysqli_connect('127.0.0.1', 'root', '', 'test');

$id = mysqli_real_escape_string($con,$_REQUEST['id']);
$query = "SELECT image_posts FROM ratepicture WHERE id = ".$id."";
$image = mysqli_query($con, $query);

if(mysqli_num_rows($image) == 0)
{
$queryError = "SELECT image FROM error_pictures WHERE id = 1";
$error = mysqli_query($con, $queryError);
$error = mysqli_fetch_assoc($error);
$error = $error['image'];
echo $error;
}else
{
$image = mysqli_fetch_assoc($image);
$image = $image['image_posts'];
echo $image;
}


?>
4

5 回答 5

2
... expects parameter 1 to be mysqli_result, boolean given in ...

几乎总是由查询失败引起的,因为mysqli_query()失败时返回 false

您需要检查您的线路/查询:

$query = "SELECT image_posts FROM ratepicture WHERE id = ".$id."";

诸如:

  • 桌子ratepictures/视图吗?
  • 它有image_posts专栏吗?
  • 的转义值是$id多少?
于 2013-04-24T07:07:10.537 回答
1

使用它来检查查询错误

$image = mysqli_query($con, $query) OR die(mysqli_error());
于 2013-04-24T07:08:34.183 回答
0

你混淆了变量。

$id = mysqli_real_escape_string($con,$_REQUEST['id']);
$query = "SELECT image_posts FROM ratepicture WHERE id = ".$id."";
$rs = mysqli_query($con, $query);
if(mysqli_num_rows($rs) == 0) {

    $sql = "SELECT image FROM error_pictures WHERE id = 1";
    $rs = mysqli_query($con, $sql);
    $row = mysqli_fetch_assoc($rs);
    $image = $row['image'];
    echo $image;

}

不知道为什么要使用 1 个单变量 ( $error) 来应用于所有返回值。奇怪的。

于 2013-04-24T07:07:00.497 回答
0

如果用 if (isset($image)) 包装 num 行

$image 为空可能会失败

于 2013-04-24T07:07:42.627 回答
0

尝试将查询更改为此

$query = "SELECT image_posts FROM ratepicture WHERE id = '".$id."'";
于 2013-04-24T07:08:44.117 回答