简体   繁体   中英

how to check if mysql query return no result(record not found) using php?

i am passing images file names via textarea to php script to find information about each image in mysql db .The problem is i am trying to output those image file names that not found in mysql db and inform the user which image file names not found in mysql. my current code fails to output those missing records in db but it correctly outputs information about those images found in db. could any one tell me what i am doing wrong ?

foreach ($lines as $line) {


$line = rtrim($line);


$result = mysqli_query($con,"SELECT ID,name,imgUrl,imgPURL FROM testdb WHERE imgUrl like '%$line'");            



 if (!$result) {
             die('Invalid query: ' . mysql_error());
            }
//echo $result;

  if($result == 0) 
    {

       // image not found, do stuff..
      echo "Not Found Image:".$line; 
    }



while($row = mysqli_fetch_array($result))
  {
  $totalRows++;

  echo "<tr>";
  echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['imgPURL'] . "</td>";
  echo "<td>" . $row['imgUrl'] . "</td>";  echo "</tr>";


}

};

echo "</table>";

echo "<br>totalRows:".$totalRows;

You can use mysqli_num_rows() in mysqli

if(mysqli_num_rows($result) > 0){

    while($row = mysqli_fetch_array($result))
    {
        $totalRows++;

        echo "<tr>";
        echo "<td>" . $row['ID'] ."(".$totalRows. ")</td>";
        echo "<td>" . $row['name'] . "</td>";
        echo "<td>" . $row['imgPURL'] . "</td>";
        echo "<td>" . $row['imgUrl'] . "</td>";  
        echo "</tr>";         
    }
} else {
    echo "<tr><td colspan='4'>Not Found Image:".$line.'</td></tr>';
}

You want to use mysqli_num_rows

if(mysqli_num_rows($result)) {
   // Do your while loop here
}

使用mysqli_num_rows比较结果集中的行数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM