note 92593 added to function.pg-send-query

From: Date: Thu, 30 Jul 2009 16:36:32 +0000
Subject: note 92593 added to function.pg-send-query
Groups: php.notes 
Request: Send a blank email to php-notes+get-158924@lists.php.net to get a copy of this message
Regarding the failure to process them all now, then retrieve the results later, I found that all queries would return successfully if I added a further condition to your while loop in the "stack_query()" method. By adding: && (pg_transaction_status($conn) === PGSQL_TRANSACTION_IDLE ) ) Every query executed with no errors. <?php $conn = pg_connect("dbname=dbname host=localhost user=psql "); if ($conn === FALSE) exit("Can't connect to db"); $q = array(); // send some queries foreach (range(0, 50) as $i) stack_query($q, $conn, "SELECT 'query $i' AS str;"); // receive them while (true) { $left = stack_query($q, $conn); echo "$left left... "; $result = pg_get_result($conn); if ($left == 0 && $result === FALSE) break; $row = pg_fetch_assoc($result); // depending on race conditions, you wont get all your original queries here. echo "got $row[str]\n"; } function stack_query(&$queries, $conn, $sql = FALSE) { if ($sql !== FALSE) $queries[] = $sql; while (count($queries) && !pg_connection_busy($conn) && (pg_transaction_status($conn) === PGSQL_TRANSACTION_IDLE ) ) pg_send_query($conn, array_shift($queries)); return count($queries) + (pg_connection_busy($conn) ? 1 : 0); } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 166.67.64.21 ---- Manual Page -- http://www.php.net/manual/en/function.pg-send-query.php Edit -- https://master.php.net/note/edit/92593 Del: integrated -- https://master.php.net/note/delete/92593/integrated Del: useless -- https://master.php.net/note/delete/92593/useless Del: bad code -- https://master.php.net/note/delete/92593/bad+code Del: spam -- https://master.php.net/note/delete/92593/spam Del: non-english -- https://master.php.net/note/delete/92593/non-english Del: in docs -- https://master.php.net/note/delete/92593/in+docs Del: other reasons-- https://master.php.net/note/delete/92593 Reject -- https://master.php.net/note/reject/92593 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#158924) next »