Unbuffered queries using Postgres+PHP?
Is there a way to execute unbuffered queries against PostgreSQL from PHP, or at least limit the size开发者_运维百科 of the local buffer that a driver uses?
I far as I could tell from the time I looked into this, there are no unbuffered queries using the pgsql driver on PHP. But the following code can help you out with this problem:
$curSql = "DECLARE cursor1 CURSOR FOR SELECT * FROM big_table";
$con = new PDO("pgsql:host=whatever dbname=whatever", "user", "pass");
$con->beginTransaction(); // cursors require a transaction.
$stmt = $con->prepare($curSql);
$stmt->execute();
$innerStatement = $con->prepare("FETCH 1 FROM cursor1");
while($innerStatement->execute() && $row = $innerStatement->fetch(PDO::FETCH_ASSOC)) {
echo $row['field'];
}
Source: http://codepoets.co.uk/2014/postgresql-unbuffered-queries/#more-936
Unfortunately there is not an unbuffered mode for the PHP pgsql extension or the pdo_pgsql extension. Flourish has an unbufferedQuery() method for fDatabase, but for PostgreSQL databases it uses the normal query functionality of the extensions.
Hmmm... I found a PHP class belonging to the Flourish (un-)framework, that seems to have a function that could suit your needs.
精彩评论