如何使用php检查mysql查询是否未返回结果(找不到记录)? [英] how to check if mysql query return no result(record not found) using php?

查看:142
本文介绍了如何使用php检查mysql查询是否未返回结果(找不到记录)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过textarea将图像文件名传递给php脚本以查找有关mysql db中每个图像的信息.问题是我正在尝试输出在mysql db中找不到的那些图像文件名,并通知用户哪个图像文件名在mysql中找不到.我当前的代码无法在db中输出那些丢失的记录,但可以正确输出有关在db中找到的那些图像的信息.谁能告诉我我做错了什么?

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;

解决方案

您可以在mysqli中使用mysqli_num_rows()

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

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

这篇关于如何使用php检查mysql查询是否未返回结果(找不到记录)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆