大家好,我正在尝试通过mySQL和php运行多个查询,但到目前为止还无法弄清楚……我读到mysqli_multi_query可能就是我想要的,并且仅在第一个查询被执行的情况下也尝试了运行,因为我使用了这种格式。 “ query”;“ query”;“ query”等...所以我的最后一次尝试是这样的“ query; query; query”等,但是没有人可以请他弄清楚让所有4个查询连续运行的语法。首先是查询,然后是php代码。
INSERT INTO queue (SELECT NULL,item1, log, Active FROM shipping WHERE Active = 1);
DELETE FROM shipping ORDER BY id ASC LIMIT 1;
UPDATE shipping SET Active ='0' ORDER BY id DESC LIMIT 1;
UPDATE shipping SET Active ='1' ORDER BY id ASC LIMIT 1;
这是php。
<?php
$servername = "localhost";
$database = "logistics";
$username = "root";
$password = "";
$conn = mysqli_connect($servername, $username, $password, $database);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO queue (SELECT NULL,item1, log, Active FROM shipping WHERE Active = 1);
DELETE FROM shipping ORDER BY id ASC LIMIT 1;
UPDATE shipping SET Active ='0' ORDER BY id DESC LIMIT 1;
UPDATE shipping SET Active ='1' ORDER BY id ASC LIMIT 1"
if (mysqli_multi_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>
先感谢
答案 0 :(得分:1)
除了已经注释过的UPDATE
语句中的错误语法外,您还应考虑将这些SQL代码拉入存储过程并从客户端代码中调用该过程,例如
create procedure usp_doSome
as
begin
INSERT INTO queue (SELECT NULL,item1, log, Active FROM shipping WHERE Active = 1);
DELETE FROM shipping ORDER BY id ASC LIMIT 1;
UPDATE shipping SET Active ='0' ORDER BY id DESC LIMIT 1;
UPDATE shipping SET Active ='1' ORDER BY id ASC LIMIT 1;
end
关于如何从php调用存储过程的知识,这里有许多示例。见下文
http://www.mysqltutorial.org/php-calling-mysql-stored-procedures/