array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.pg-send-query.php', 1 => 'pg_send_query', ), 'up' => array ( 0 => 'ref.pgsql.php', 1 => 'PostgreSQL 函数', ), 'prev' => array ( 0 => 'function.pg-send-query-params.php', 1 => 'pg_send_query_params', ), 'next' => array ( 0 => 'function.pg-set-client-encoding.php', 1 => 'pg_set_client_encoding', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/pgsql/functions/pg-send-query.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_send_query — 发送异步查询
pg_send_query() 向 connection
发送异步查询。跟 pg_query()
不同,它可以一次性向 PostgreSQL 发送多个查询并用 pg_get_result() 依次得到结果。
当执行查询时不会阻塞脚本执行。用 pg_connection_busy() 来检查连接是否繁忙(即查询正在执行中)。可以使用 pg_cancel_query() 取消查询。
尽管用户可以同时发送多个查询,但连接繁忙的情况下无法发送多个查询。如果连接繁忙时发送了查询,则会等待上一个查询结束,并且丢弃上个查询的所有结果。
成功时返回 true
,失败时返回 false
或 0
。使用 pg_get_result()
来确定查询结果。
版本 | 说明 |
---|---|
8.1.0 |
现在 connection 参数接受 PgSql\Connection
实例,之前接受 resource。
|
示例 #1 pg_send_query() 示例
<?php
$dbconn = pg_connect("dbname=publisher") or die("Could not connect");
if (!pg_connection_busy($dbconn)) {
pg_send_query($dbconn, "select * from authors; select count(*) from authors;");
}
$res1 = pg_get_result($dbconn);
echo "First call to pg_get_result(): $res1\n";
$rows1 = pg_num_rows($res1);
echo "$res1 has $rows1 records\n\n";
$res2 = pg_get_result($dbconn);
echo "Second call to pg_get_result(): $res2\n";
$rows2 = pg_num_rows($res2);
echo "$res2 has $rows2 records\n";
?>
以上示例会输出:
First call to pg_get_result(): Resource id #3 Resource id #3 has 3 records Second call to pg_get_result(): Resource id #4 Resource id #4 has 1 records