Dan*_*iil 0 php mysql sql sql-delete
我每隔15分钟运行一次查询,从API检索新数据并将此数据存储在我的数据库中.
因此,每隔15分钟,我想将新数据存储在表中,并删除该表中的所有旧数据.
我目前正在使用以下方法:
$sql = "DELETE FROM self_user_follower
INSERT INTO self_user_follower (username, profile_picture, full_name, user_id, last_updated)
VALUES (:query_username, :query_profile_picture, :query_full_name, :query_user_id, :query_last_updated)";
Run Code Online (Sandbox Code Playgroud)
但它给了我以下错误:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO self_user_follower (username, profile_picture, full_name, user_id, l' at line 2
)
query_error
Run Code Online (Sandbox Code Playgroud)
这是最好的方法,还是有更好,更清洁的方法来做到这一点?
如果要将两个SQL查询放入一个语句字符串中,则必须这样做
;)分隔它们mysqli_multi_query支持多个查询的功能.除非您有充分的理由,否则请修改代码,使其分别执行每个查询.如果您需要,MySQLi提供交易支持.*
究其原因,为什么你需要一个单独的功能是有益的; 如文档中所述:
额外的API调用用于多个语句,以减少意外SQL注入攻击的可能性.攻击者可能会尝试添加诸如的语句; DROP DATABASE mysql或; 选择休眠(999).如果攻击者成功将SQL添加到语句字符串但未使用mysqli_multi_query,则服务器将不会执行第二个注入的恶意SQL语句.
*:实际上,我甚至不确定multi_query是否会在同一个事务中执行两个查询 - 我只是猜测你使用多个查询的原因.