多个准备语句的优雅解决方案
elegant solution for multiple prepare statements
我试图以更好的方式解决的问题是删除包含带有标签的图像的文件夹。所以对于每张图片我都需要删除
-图像本身
-来自三个数据库的该图像的标签(img_offer、img_member、img_horses)
目前我得到了要删除的文件夹的所有图像 ID,然后用四个不同的查询迭代了这四次,这看起来效率很低。
主要问题是,据我所知,您不能同时打开多个准备语句,并且在每次迭代中创建新的语句似乎也违反直觉。
我认为最好的方法是类似于多查询准备语句,但我找不到任何类似的东西,所以也许这里有人知道如何以更简洁的方式解决这个问题
我的想法是这样的
$multiplePreparedStatement= "DELETE this FROM that WHERE id=?;
DELETE this2 FROM that2 WHERE id2=?;
DELETE this3 FROM that3 WHERE id3=?;";
$preparedStmt = $conn->prepare($multiplePreparedStatement);
foreach($imgArray as $imgId){
$preparedStmt->bind_param("iii", $imgId, $imgId, $imgId);
$preparedStmt->execute();
}
$preparedStmt->close();
但我认为这不会像多个 SQL 准备好的语句中不支持查询那样工作吗?
这是我当前的代码:
$id=$_GET['deleteAlbum'];
$getImages = "SELECT image_id AS id
FROM Images
WHERE folder_id = ?";
$deleteImage="DELETE FROM Images
WHERE image_id=?";
$deleteOffer = "DELETE FROM Images_Offers
WHERE image_id=?";
$deleteHorse = "DELETE FROM Images_Horses
WHERE image_id=?";
$deleteTeam = "DELETE FROM Images_Team
WHERE image_id=?";
//get all image ids
$ImgStmt=$conn->prepare($getImages);
$ImgStmt->bind_param("i", $id);
$ImgStmt->execute();
$ImgStmt->bind_result($id);
$imgToDelete = array();
while($ImgStmt->fetch()){
array_push($imgToDelete, $id);
}
$ImgStmt->close();
$stmt=$conn->prepare($deleteOffer);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteHorse);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteTeam);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteImage);
foreach($imgToDelete as $imgId){
unlink("../assets/img/images/img".$imgId.".jpg");
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
我也有创建多个连接的想法,但我认为这可能会出现问题,例如在我仍然有一个遍历图像的查询时删除图像。
您根本不必遍历 image_id
(至少不需要遍历 SQL 数据)。您可以一次性从数据库中删除与特定 folder_id
关联的所有内容:
DELETE Images, Images_Offers, Images_Horses, Images_Team
FROM Images
LEFT JOIN Images_Offers ON Images_Offers.image_id = Images.image_id
LEFT JOIN Images_Horses ON Images_Horses.image_id = Images.image_id
LEFT JOIN Images_Team ON Images_Team.image_id = Images.image_id
WHERE folder_id = ?;
当然,在那之前你应该unlink
实际的文件。
我试图以更好的方式解决的问题是删除包含带有标签的图像的文件夹。所以对于每张图片我都需要删除
-图像本身
-来自三个数据库的该图像的标签(img_offer、img_member、img_horses)
目前我得到了要删除的文件夹的所有图像 ID,然后用四个不同的查询迭代了这四次,这看起来效率很低。
主要问题是,据我所知,您不能同时打开多个准备语句,并且在每次迭代中创建新的语句似乎也违反直觉。
我认为最好的方法是类似于多查询准备语句,但我找不到任何类似的东西,所以也许这里有人知道如何以更简洁的方式解决这个问题
我的想法是这样的
$multiplePreparedStatement= "DELETE this FROM that WHERE id=?;
DELETE this2 FROM that2 WHERE id2=?;
DELETE this3 FROM that3 WHERE id3=?;";
$preparedStmt = $conn->prepare($multiplePreparedStatement);
foreach($imgArray as $imgId){
$preparedStmt->bind_param("iii", $imgId, $imgId, $imgId);
$preparedStmt->execute();
}
$preparedStmt->close();
但我认为这不会像多个 SQL 准备好的语句中不支持查询那样工作吗?
这是我当前的代码:
$id=$_GET['deleteAlbum'];
$getImages = "SELECT image_id AS id
FROM Images
WHERE folder_id = ?";
$deleteImage="DELETE FROM Images
WHERE image_id=?";
$deleteOffer = "DELETE FROM Images_Offers
WHERE image_id=?";
$deleteHorse = "DELETE FROM Images_Horses
WHERE image_id=?";
$deleteTeam = "DELETE FROM Images_Team
WHERE image_id=?";
//get all image ids
$ImgStmt=$conn->prepare($getImages);
$ImgStmt->bind_param("i", $id);
$ImgStmt->execute();
$ImgStmt->bind_result($id);
$imgToDelete = array();
while($ImgStmt->fetch()){
array_push($imgToDelete, $id);
}
$ImgStmt->close();
$stmt=$conn->prepare($deleteOffer);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteHorse);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteTeam);
foreach ($imgToDelete as $imgId){
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
$stmt=$conn->prepare($deleteImage);
foreach($imgToDelete as $imgId){
unlink("../assets/img/images/img".$imgId.".jpg");
$stmt->bind_param("i",$imgId);
$stmt->execute();
}
$stmt->close();
我也有创建多个连接的想法,但我认为这可能会出现问题,例如在我仍然有一个遍历图像的查询时删除图像。
您根本不必遍历 image_id
(至少不需要遍历 SQL 数据)。您可以一次性从数据库中删除与特定 folder_id
关联的所有内容:
DELETE Images, Images_Offers, Images_Horses, Images_Team
FROM Images
LEFT JOIN Images_Offers ON Images_Offers.image_id = Images.image_id
LEFT JOIN Images_Horses ON Images_Horses.image_id = Images.image_id
LEFT JOIN Images_Team ON Images_Team.image_id = Images.image_id
WHERE folder_id = ?;
当然,在那之前你应该unlink
实际的文件。