pg_send_query

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_send_query 發(fā)送異步查詢(xún)

說(shuō)明

pg_send_query(resource $connection, string $query): bool
pg_send_query(string $query): bool

pg_send_query()connection 連接發(fā)送異步查詢(xún)。和 pg_query() 不同,它可以向 PostgreSQL 發(fā)送多個(gè)查詢(xún)并用 pg_get_result() 依次得到結(jié)果。當(dāng)執(zhí)行查詢(xún)時(shí)腳本的執(zhí)行不會(huì)被鎖定。用 pg_connection_busy() 來(lái)檢查連接連接是否為忙(即查詢(xún)正在執(zhí)行中)。調(diào)用 pg_cancel_query() 則有可能取消查詢(xún)。

盡管用戶(hù)可以一次發(fā)送多個(gè)查詢(xún),但用戶(hù)不能通過(guò)正忙的連接發(fā)送多個(gè)查詢(xún)。如果向正忙的連接發(fā)送了查詢(xún),則會(huì)等待上一條查詢(xún)結(jié)束并丟棄所有結(jié)果。

示例 #1 異步查詢(xún)

<?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()。