我正在尝试检查文件夹中是否存在文件,如果不存在,我想更新我的 sql 数据库。到目前为止,我正在遍历一个 id 数组,该数组应该有一个与数据库中的文件号相对应的数字。这意味着我多次查询 sql 数据库而不是一次。只进行一次查询是否可能且效率更高?这是我的代码的样子:
$photo_status = "SELECT id FROM photo_set_table WHERE Photo_added = 1";
$photo_added = mysql_query($photo_status);
if (!$photo_added) {
die('Invalid query: ' . mysql_error());
}
$path = '/home/aXXXXXXX/public_html/';
$path .= '/images_android/images/';
$no_photo_array = array();
while($added = mysql_fetch_array($photo_added))
{
if(!file_exists($path.$added[0].'.jpg')){
array_push($no_photo_array, $added[0]);
}
}//while added
$count_changes = 0;
foreach ($no_photo_array as $value) {
$change_status = "UPDATE photo_set_table SET Photo_added = 0 WHERE id = $value";
$photo_added2 = mysql_query($change_status);
if (!$photo_added2) {
die('Invalid query: ' . mysql_error());
} else {
$count_changes++;
}
} //foreach ($no_photo_array....
echo "The number of affected lines is: ".$count_changes;
mysql_close($connection);