通过php运行许多mySQL查询

通过php运行许多mySQL查询

This question already has answers here:
Why I cannot run two mysqli queries? The second one fails [duplicate]
(3个答案)
三年前关闭。
我有一个有很多表的数据库。在第1面,我有一个按钮把我转到第2面。在第2方面,我希望代码删除每个命名表中的每一行,这些表中有一个特定的pid存储在一个变量中。我在第二面有一个代码,但它什么也做不了。
第二面代码:
$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3'";
$del .= "DELETE FROM project WHERE pid='$pid3'";
$del .= "DELETE FROM question WHERE pid='$pid3'";
$del .= "DELETE FROM respondent WHERE pid='$pid3'";
$del .= "DELETE FROM result WHERE pid='$pid3'";
$del .= "DELETE FROM users WHERE pid='$pid3'";

$run = mysqli_query($mysqli,$del);

if($run)
{
   echo "<h1>Project is deleted!</h1>";
}

最佳答案

使用mysqli_multi_query

$pid3 = $_POST['pid2'];

$del = "DELETE FROM answer_det WHERE pid='$pid3';";
$del .= "DELETE FROM project WHERE pid='$pid3';";
$del .= "DELETE FROM question WHERE pid='$pid3';";
$del .= "DELETE FROM respondent WHERE pid='$pid3';";
$del .= "DELETE FROM result WHERE pid='$pid3';";
$del .= "DELETE FROM users WHERE pid='$pid3';";

$run = mysqli_multi_query($mysqli,$del);

if($run)
{
   echo "<h1>Project is deleted!</h1>";
}

关于php - 通过php运行许多mySQL查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34441108/

10-12 17:17