php mysql查询有无记录,如何使用php检查mysql查询是否未返回结果(找不到记录)?...

博主正在通过textarea将图片文件名传递给PHP脚本,以查询MySQL数据库中关于每张图片的信息。问题在于,他想输出那些在数据库中找不到的图片文件名,并通知用户哪些文件名缺失。目前的代码只能正确输出数据库中存在的图片信息,但无法显示未找到的记录。解决方案是使用mysqli_num_rows()检查查询结果,如果结果大于0则输出图片信息,否则提示图片未找到。
摘要由CSDN通过智能技术生成

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 "

";

echo "

" . $row['ID'] ."(".$totalRows. ")";

echo "

" . $row['name'] . "";

echo "

" . $row['imgPURL'] . "";

echo "

" . $row['imgUrl'] . ""; echo "";

}

};

echo "";

echo "
totalRows:".$totalRows;

解决方案

You can use mysqli_num_rows() in mysqli

if(mysqli_num_rows($result) > 0){

while($row = mysqli_fetch_array($result))

{

$totalRows++;

echo "

";

echo "

" . $row['ID'] ."(".$totalRows. ")";

echo "

" . $row['name'] . "";

echo "

" . $row['imgPURL'] . "";

echo "

" . $row['imgUrl'] . "";

echo "

";

}

} else {

echo "

Not Found Image:".$line.'';

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值