0

Hi I was doing a check if the fetch array will give me 0 rows of data so I use the num_row() but i always give me an error

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

HERE is my code that I used

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


?>
rodolfo navalon
  • 191
  • 1
  • 1
  • 12
  • If you're already using MySQLi, you should have used prepared statement and variable binding, instead of `real_escape_string` and stuffing everything into a SQL query. – Passerby Apr 24 '13 at 07:07
  • `mysqli_query($con, $query);` is returning FALSE, your query is not being executed – Hanky Panky Apr 24 '13 at 07:08
  • @Passerby not a must. it's perfectly okay to use `real_escape_string`. Prepared statement is much slower. – Raptor Apr 24 '13 at 07:10
  • @ShivanRaptor Didn't know that. Can you provide some link about the "much slower" issue? – Passerby Apr 24 '13 at 07:12
  • @Passerby i did a benchmark on querying the same SQL using prepared statement versus escaped statement. escaped statement is 3 times faster than prepared statement. – Raptor Apr 24 '13 at 07:13
  • 1
    @Passerby It actually disproves the point but: http://stackoverflow.com/a/6301789/1745573 Shivan, did you also use `mysqli_real_escape_string` in your personal benchmark? Using non-pdo is marginally faster, but not really enough to make a deciding decision between the two. The difference is really only seen when using a lot of queries, in which case, prepared queries would win out if you are just using different data, but the same query. – Jon Apr 24 '13 at 07:13

5 Answers5

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

is pretty much always caused by failure of a query, because mysqli_query() returns false on failure.

You need to check your line/query:

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

Things such as:

  • Do you have a ratepictures table/view?
  • Does it have an image_posts column?
  • What is the escaped value of $id?
paxdiablo
  • 814,905
  • 225
  • 1,535
  • 1,899
1

Use this for checking the query error

$image = mysqli_query($con, $query) OR die(mysqli_error());
Yogesh Suthar
  • 30,136
  • 18
  • 69
  • 98
  • 2
    That's good advice for development, not sure I'd want it in production but it's useful in finding out where the problem lies. – paxdiablo Apr 24 '13 at 07:14
0

You mixed up the variable.

$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;

}

Don't know why you want to use 1 single variable ( $error ) to apply to all return values. Strange.

Raptor
  • 51,208
  • 43
  • 217
  • 353
0

Wrap the num rows if with an if (isset($image))

It could be failing on $image being null

0

Try changing query to this

$query = "SELECT image_posts FROM ratepicture WHERE id = '".$id."'";
chandresh_cool
  • 11,600
  • 3
  • 27
  • 44