(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_send_query — 發(fā)送異步查詢
$connection
, string $query
): bool$query
): bool
pg_send_query() 向 connection
連接發(fā)送異步查詢。和 pg_query() 不同,它可以向 PostgreSQL
發(fā)送多個(gè)查詢并用 pg_get_result()
依次得到結(jié)果。當(dāng)執(zhí)行查詢時(shí)腳本的執(zhí)行不會(huì)被鎖定。用
pg_connection_busy() 來(lái)檢查連接連接是否為忙(即查詢正在執(zhí)行中)。調(diào)用
pg_cancel_query() 則有可能取消查詢。
盡管用戶可以一次發(fā)送多個(gè)查詢,但用戶不能通過(guò)正忙的連接發(fā)送多個(gè)查詢。如果向正忙的連接發(fā)送了查詢,則會(huì)等待上一條查詢結(jié)束并丟棄所有結(jié)果。
示例 #1 異步查詢
<?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
參見(jiàn) pg_query(),pg_cancel_query(),pg_get_result() 和 pg_connection_busy()。