mysqli行数总是给我一个错误

时间:2013-04-24 07:04:56

标签: php

嗨我正在检查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;
}


?>

5 个答案:

答案 0 :(得分:2)

... expects parameter 1 to be mysqli_result, boolean given in ...
由于查询失败,

几乎总是,因为mysqli_query() returns false on failure

您需要检查自己的行/查询:

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

如:

  • 有<{1}}表/视图吗?
  • 是否有ratepictures列?
  • image_posts的转义价值是什么?

答案 1 :(得分:1)

使用它来检查查询错误

$image = mysqli_query($con, $query) OR die(mysqli_error());

答案 2 :(得分: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)来应用所有返回值。奇怪。

答案 3 :(得分:0)

如果使用if(isset($ image))

,则包裹num行

在$ image为null时可能会失败

答案 4 :(得分:0)

尝试将查询更改为此

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