multiple php,PHP Multiple Statements

Multiple Statements

MySQL optionally allows having multiple statements in one statement string.

Sending multiple statements at once reduces client-server

round trips but requires special handling.

Multiple statements or multi queries must be executed

with

The MySQL server allows having statements that do return result sets and

statements that do not return result sets in one multiple statement.

Example #1 Multiple Statements

$mysqli= newmysqli("example.com","user","password","database");

if ($mysqli->connect_errno) {

echo"Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;

}

if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {

echo"Table creation failed: (".$mysqli->errno.") ".$mysqli->error;

}$sql="SELECT COUNT(*) AS _num FROM test; ";$sql.="INSERT INTO test(id) VALUES (1); ";$sql.="SELECT COUNT(*) AS _num FROM test; ";

if (!$mysqli->multi_query($sql)) {

echo"Multi query failed: (".$mysqli->errno.") ".$mysqli->error;

}

do {

if ($res=$mysqli->store_result()) {var_dump($res->fetch_all(MYSQLI_ASSOC));$res->free();

}

} while ($mysqli->more_results() &&$mysqli->next_result());?>

以上例程会输出:

array(1) {

[0]=>

array(1) {

["_num"]=>

string(1) "0"

}

}

array(1) {

[0]=>

array(1) {

["_num"]=>

string(1) "1"

}

}

Security considerations

The API functions ; DROP DATABASE mysql or ; SELECT SLEEP(999).

If the attacker succeeds in adding SQL to the statement string but

mysqli_multi_query is not used, the server will not

execute the second, injected and malicious SQL statement.

Example #2 SQL Injection

$mysqli= newmysqli("example.com","user","password","database");$res=$mysqli->query("SELECT 1; DROP TABLE mysql.user");

if (!$res) {

echo"Error executing query: (".$mysqli->errno.") ".$mysqli->error;

}?>

以上例程会输出:

Error executing query: (1064) 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 'DROP TABLE mysql.user' at line 1

Prepared statements

Use of the multiple statement with prepared statements is not supported.

See also

mysqli_result::next-result()

mysqli_result::more-results()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值