gpt4 book ai didi

php - PHP 中的 CONCAT SQL 查询

转载 作者:行者123 更新时间:2023-11-29 05:15:23 25 4
gpt4 key购买 nike

这是我的代码,但出现 SQL 语法错误;

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area1."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area2."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area3."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area4."')";
$insert .= "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area5."')";

执行此查询的正确方法是什么?

最佳答案

当插入多个元组时,这是使用的表示法。

$insert = "INSERT IGNORE INTO locations (location_id, location_name) VALUES (NULL, '".$area1."')";
$insert .= ", (NULL, '".$area2."')";
$insert .= ", (NULL, '".$area3."')";
$insert .= ", (NULL, '".$area4."')";
$insert .= ", (NULL, '".$area5."')";
$insert .= ";";

提供多个 INSERT 子句建议单独查询;根据 MySQL 库的不同,这可能是可以接受的,但每个都需要 ; 终止。

也就是说,您确实应该使用参数化查询。当 prepared 时,它们的速度通常与此相当(一些较大的批量插入除外)。

关于php - PHP 中的 CONCAT SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33616103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com