开发者

Cannot execute two mysql queries at once in PHP

开发者 https://www.devze.com 2022-12-21 05:43 出处:网络
If I try to execute two MySQL queries at once by delimiting them with a semicolon, it works fine in PHP MyAdmin, but the query doesn\'t work at all in PHP mysql_query().

If I try to execute two MySQL queries at once by delimiting them with a semicolon, it works fine in PHP MyAdmin, but the query doesn't work at all in PHP mysql_query().

Does 开发者_C百科anyone know why?


mysql_query only allows you to make one query at a time. This helps prevent SQL injection.

From the docs:

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

If what you're trying to do is make sure that the two queries run in one transaction, check out this article on transaction control with PHP and MySQL.


This might explain/help: http://joseph.randomnetworks.com/archives/2005/10/11/guide-to-php-security/


For security reasons, you can only run one query at a time. You could cycle through an array of queries though, and run each independently.

$queries[] = "INSERT INTO users (name) VALUES ('jonathan')";
$queries[] = "UPDATE users SET name = 'Jonathan' WHERE name = 'jonathan'";

foreach ($queries as $query) {
  mysql_query($query);
}

You could even explode a single string containing two queries into an array:

$sql = "INSERT INTO users (name) values ('jonathan');
        INSERT INTO users (name) values ('sampson')";

$queries = explode(";", $sql);

Where $queries would become an array of queries which we could then cycle through.

0

精彩评论

暂无评论...
验证码 换一张
取 消