pg_send_query
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_send_query — 發送非同步查詢
說明
pg_send_query(resource
$connection
, string $query
): boolpg_send_query(string
$query
): bool
pg_send_query() 向 connection
連線發送非同步查詢。和 pg_query() 不同,它可以向 PostgreSQL
發送多個查詢並用 pg_get_result()
依次得到結果。當執行查詢時指令碼的執行不會被鎖定。用
pg_connection_busy() 來檢查連線連線是否為忙(即查詢正在執行中)。呼叫
pg_cancel_query() 則有可能取消查詢。
儘管使用者可以一次發送多個查詢,但使用者不能通過正忙的連線發送多個查詢。如果向正忙的連線發送了查詢,則會等待上一條查詢結束並丟棄所有結果。
示例 #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
參見 pg_query(),pg_cancel_query(),pg_get_result() 和 pg_connection_busy()。