Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...