我通过textarea将图像文件名传递给php脚本,以在mysqldb中查找每个图像的信息。问题是我试图输出那些在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>';
}
您想要使用mysqli_num_rows
if(mysqli_num_rows($result)) {
// Do your while loop here
}
使用mysqli_num_rows
比较结果集中的行数。