[SOLVED] What's wrong with my query — mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in

Issue

I know that the above message occurs becomes of wrong MySQL query, but what’s wrong with my MySQL query here in this code?
I did a lot of research to find it, but couldn’t find the resolution.
Table name: pic_msg
Columns: image_path, ip, index(auto_increment)

<?php
$con = mysqli_connect("localhost","root","password","my_database");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>


<?php
$test_ip = "test-ip";

$sql = "SELECT image_path FROM pic_msg  WHERE ip = '$test_ip' ORDER BY index DESC LIMIIT 1";
$result = mysqli_query($con, $sql);
$rowcount = mysqli_num_rows($result);
if ($rowcount > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["image_path"]."<br>";
    }
} else {

}

mysqli_close($con);
?>

Solution

index is a reserved word – you should escape it. Additionally, you have a typo – it’s limit, not limiit:

$sql = "SELECT `image_path` FROM `pic_msg` WHERE `ip` = '$test_ip' ORDER BY `index` DESC LIMIT 1";

Answered By – Mureinik

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published.