如何在一个 mysql_query 中执行多个 SQL 语句? [英] How do you do multiple SQL statements in one mysql_query?
问题描述
说我想做 UPDATE table SET name = 'bob'
和 UPDATE table SET age = 55 WHERE name = 'jim'
我如何在相同的mysql_query()?
Say I wanted to do UPDATE table SET name = 'bob'
and UPDATE table SET age = 55 WHERE name = 'jim'
how do I do them in the same mysql_query()?
由于这个问题有相当多的观点,我想指出,从 PHP 5.5 mysql_query
和其他 mysql_*
函数现已弃用,不应使用.
Since this question has a fair amount of views I'd like to point out that as of PHP 5.5 mysql_query
and other mysql_*
functions are now deprecated and shouldn't be used.
推荐答案
这个我没试过,不过我觉得你可以用 mysqli::multi_query.mysql_query
拒绝多个语句的好处之一是它立即排除了一些更常见的 SQL 注入攻击,例如添加 ';DELETE FROM ... #
到一个语句.因此,您可能需要小心处理多个语句.
I've never tried this, but I think you can use mysqli::multi_query. One of the good things about mysql_query
rejecting multiple statements is that it immediately rules out some of the more common SQL injection attacks, such as adding '; DELETE FROM ... #
to a statement. You might therefore want to be careful with multiple statements.
这篇关于如何在一个 mysql_query 中执行多个 SQL 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!