mysqli::query
(PHP 5, PHP 7)
mysqli :: query - mysqli_query - 对数据库执行查询
描述
面向对象的风格
mixed mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
程序风格
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
对数据库执行一次query
。
对于非DML查询(不是INSERT,UPDATE或DELETE),该函数类似于调用mysqli_real_query(),后跟mysqli_use_result()或mysqli_store_result()。
注意:如果将语句传递给服务器的max_allowed_packet的语句长度超过mysqli_query(),则返回的错误代码根据您使用的是MySQL本机驱动程序(mysqlnd)还是MySQL客户端库(libmysqlclient)而有所不同。行为如下:
- Linux上的mysqlnd 返回一个1153的错误代码。错误消息表示“得到的数据包大于max_allowed_packet字节”。
- Windows上的mysqlnd返回错误代码2006.此错误消息意味着“服务器已经消失”。
- 所有平台上的ibmysqlclient都会返回错误代码2006.
参数
link
仅以过程化样式:由mysqli_connect() 或 mysqli_init() 返回的链接标识。
query
查询字符串。
查询中的数据应正确转义。
resultmode
要么是常数,MYSQLI_USE_RESULT
要么 MYSQLI_STORE_RESULT
取决于期望的行为。默认情况下,MYSQLI_STORE_RESULT
使用。
如果你使用MYSQLI_USE_RESULT
所有后续的调用,将会返回错误的命令不同步,除非你调用mysqli_free_result()
用MYSQLI_ASYNC
(可用于mysqlnd),可以异步执行查询。 然后使用mysqli_poll()从这些查询中获取结果。
返回值
失败时返回 FALSE
,通过mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回TRUE
。
更新日志
版本 | 说明 |
---|---|
5.3.0 | 增加了异步查询的功能。 |
范例
示例#1 mysqli :: query()示例
面向对象风格
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
/* Create table doesn't return a resultset */
if ($mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", $result->num_rows);
/* free result set */
$result->close();
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can't execute any functions which interact with the
server until result set was closed. All calls will return an
'out of sync' error */
if (!$mysqli->query("SET @a:='this will not work'")) {
printf("Error: %s\n", $mysqli->error);
}
$result->close();
}
$mysqli->close();
?>
过程化风格
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Create table doesn't return a resultset */
if (mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City") === TRUE) {
printf("Table myCity successfully created.\n");
}
/* Select queries return a resultset */
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
}
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
if ($result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT)) {
/* Note, that we can't execute any functions which interact with the
server until result set was closed. All calls will return an
'out of sync' error */
if (!mysqli_query($link, "SET @a:='this will not work'")) {
printf("Error: %s\n", mysqli_error($link));
}
mysqli_free_result($result);
}
mysqli_close($link);
?>
以上例程会输出:
表myCity成功创建。
选择返回的10行。
错误:命令不同步; 你现在不能运行这个命令
参见
- mysqli_real_query() - 执行一个SQL查询
- mysqli_multi_query() - 对数据库执行查询
- mysqli_free_result() - 释放与结果关联的内存
← mysqli::prepare
mysqli::real_connect →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com