一次执行多个或三个查询

时间:2015-10-19 05:18:06

标签: php multi-query

我目前正在创建一个在线预订系统。

如何一次执行多个查询?

我已经在使用mysqli_muti_query了。我有三个查询要执行。

<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' "; ?>

3 个答案:

答案 0 :(得分:0)

您需要在每个语句后添加分号:

<?php
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' ;"; 

答案 1 :(得分:0)

如果您的数据库支持,我建议您单独运行查询并将它们包装到单个事务中。

$conn->begin_transaction();
$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";
$conn->query($sql);

$sql = "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye';";
$conn->query($sql);

$sql = "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate';";
$conn->query($sql);

$conn->commit();

我还建议您使用带参数的预准备语句,而不是将值直接包含在查询字符串中。

答案 2 :(得分:0)

您可以使用PHP mysqli_multi_query-函数。

$sql = "INSERT INTO db (name,picuploc,lname,pnumber,age,email,address,city,zipcode,ticketid,pickuptime,dropofftime,dropoffloc,vehicle,plateno,totalprice,date_registered) VALUES ('$first','$pickuploc','$lname','$pnumber','$age','$email','$address','$city','$zip','$ticket','$pickup','$dropoff','$dropoffloc','$car','$plate','$all', NOW());";

$sql .= "UPDATE vehicles set quantity = quantity - $age WHERE vehicle = '$car' AND parent = '$ye' ;";

$sql .= "UPDATE vehicles set status = '$un'  WHERE plateno = '$plate' ;"; 

/* execute multi query */
if ($mysqli->multi_query($query)) {

}