我是使用PHP运行SQL命令的新手,但我尝试做的是在运行脚本时截断数据库中的特定表。我可以很好地截断一个表,但当我尝试多个表时,我遇到了问题!代码如下,任何指针?!提前致谢
<?php
var_dump($_POST);
$myServer = $_POST['host'];
$myUser = $_POST['user'];
$myPass = $_POST['password'];
$myDB = $_POST['db'];
$con = mysqli_connect($myServer, $myUser, $myPass) or die("Connection
Failed");
mysqli_select_db($con, $myDB)or die("Connection Failed");
$query = ("
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table customers;
TRUNCATE table customers2;
SET FOREIGN_KEY_CHECKS = 1;
");
if(mysqli_query($con, $query)){
echo "table empty";}
else{
echo("Error description: " . mysqli_error($con));}
?>
答案 0 :(得分:2)
一次执行一个查询
mysqli_query($con, "SET FOREIGN_KEY_CHECKS = 0;");
mysqli_query($con, "TRUNCATE table customers;");
mysqli_query($con, "TRUNCATE table customers2;");
mysqli_query($con, "SET FOREIGN_KEY_CHECKS = 1;");
或使用mysqli_multi_query
mysqli_multi_query($con, "
SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE table customers;
TRUNCATE table customers2;
SET FOREIGN_KEY_CHECKS = 1;
");
答案 1 :(得分:0)
mysqli_query
一次只允许一个查询。如果您想一次使用多个查询,请使用mysqli_multi_query
。文档:http://php.net/manual/en/mysqli.multi-query.php
在您的代码中,您将更改
mysqli_query($con, $query)
到
mysqli_multi_query($con, $query)