473,387 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Cursors and PHP

I found very little information on the web about how to return a cursor
from plpgsql to PHP using PEAR::DB. Here's what I managed to tack
together. It works but I want to make sure I'm not overlooking the
obvious or doing something boneheaded here, like setting myself up for a
database crash under load. Here's an example:

========================
PLPGSQL:

CREATE OR REPLACE FUNCTION get_user_data(REFCURSOR, INTEGER)
RETURNS REFCURSOR AS '

DECLARE
v_cursor ALIAS FOR $1;
v_user_id ALIAS FOR $2;

BEGIN

OPEN v_cursor FOR
SELECT *
FROM users
WHERE user_id = v_user_id;

RETURN v_cursor;

END;

' LANGUAGE 'plpgsql';

==========================
PHP:

$cursor = 'my_user';

$query = "SELECT get_user_data('$cursor', $user_id);FETCH ALL IN
$cursor;";

$res =& $dbh->query($query);

if (DB::isError($res)) {
... process the error
}

$row = $res->fetchRow(DB_FETCHMODE_ASSOC);

$dbh->query("CLOSE $cursor;");

$res->free();

return $row;
============================

Question #1: Is this the way to do it?

Question #2: I'm using a persistent database connection. To avoid
leaving an open cursor laying around I make another call to close the
cursor after the result set is fetched. Do I need to do this or will
the cursor be closed implicitly by Apache/PHP after the page is delivered?

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #1
3 3724
Steve Manes <sm****@magpie.com> writes:
$query = "SELECT get_user_data('$cursor', $user_id);FETCH ALL IN
$cursor;"; $res =& $dbh->query($query);
Hm. This will return two different, not-compatible result sets (the
SELECT's output and then the FETCH's). I don't know how PHP is defined
to react to that. It seems that it's just discarding the first result
set and giving you the second, but is that specified behavior or is it
a bug that may get fixed someday? Might be best to do this in two
queries.
Question #2: I'm using a persistent database connection. To avoid
leaving an open cursor laying around I make another call to close the
cursor after the result set is fetched. Do I need to do this or will
the cursor be closed implicitly by Apache/PHP after the page is delivered?


The backend will implicitly close the cursor at transaction end. If the
connection-pooling code is set up to issue a COMMIT after the page is
processed, you need not close the cursor separately.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
Tom Lane wrote:
Steve Manes <sm****@magpie.com> writes:
$query = "SELECT get_user_data('$cursor', $user_id);FETCH ALL IN
$cursor;";


$res =& $dbh->query($query);

Hm. This will return two different, not-compatible result sets (the
SELECT's output and then the FETCH's).


Without the FETCH in that compound PHP query however, nothing gets
returned from the SELECT. I'm not sure why but maybe it's because
PEAR::DB doesn't have any direct support for cursors so it's just
grabbing a result set.


---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #3

I'm not sure why you're doing it the exact way you are, but you basically
just call the same commands within a pg_query as you would on the psql
command line to make it work:

begin
declare mycurs cursor as select * from table
fetch 10
rollback / commit


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alban Hertroys | last post by:
Oh no! It's me and transactions again :) I'm not really sure whether this is a limitation of psycopg or postgresql. When I use multiple cursors in a transaction, the records inserted at the...
22
by: T.S.Negi | last post by:
Hi All, I want to avoid using cursors and loops in stored procedures. Please suggest alternate solutions with example (if possible). Any suggestion in these regards will be appreciated. ...
5
by: Todd Huish | last post by:
I have noticed something disturbing when retrieving datasets over a relatively slow line (multiple T1). I am looking at about 25 seconds to retrieve 500 rows via a php-odbc link. This same select...
6
by: a | last post by:
Hello, I am doing some multithreading in an MDI app, and I can't seem to get the cursor to stay as an Hourglass. I call: Cursor.Current = cursors.wait at the beginning of my routing, and...
10
by: Just Me | last post by:
Does Me.Cursor.Current=Cursors.WaitCursor set the current property of Me.Cursor to Cursors.WaitCursor And Me.Cursor.Current=Cursors.Default set the Me.Current property to something (default)...
5
by: Boni | last post by:
Dear all, 1.Is there a standard set of cursors in windows which can be used? If yes, where. I need a "hand"- cursor for drag-drop operation. 2. What is a best practice to place cursors? In app...
7
by: H. Williams | last post by:
I know the .Net Cursor class doesn't work with color cursors. So I'm currently using the LoadCursorFromFile API with reflection to set color cursors: here is my code: public static extern...
17
by: vishal | last post by:
I am new to sql and require some help on cursors? what are they and how and why are they used for??? it will be kind enough if anyone helps me in this regards.. regards vishal jain.
3
by: schwartzenberg | last post by:
Dear friends, I have just run into a strange DB2 problem. Something i'd some of you would answer, if only shortly. My basic question is: How do i ensure 'insensitive' (ie static) cursors...
1
by: Dima Kuchin | last post by:
Hello, I was trying to find the information about when and where should I use cursors in DB2, no luck. Maybe you can point me to some article that describes just that (or tell me which page is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.