Thread: PHP and PostgreSQL
Hi! I have some questions about coding in php with postgre. Here is my code $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); $row = 0; // postgres needs a row counter other dbs might not while ($data = @pg_fetch_object ($qu, $row)) { echo $data->username." ("; echo $data->password ."): "; echo $data->client_id."<BR>"; $row++; } When i execute this i get 3 records (in DB is also 3 records), if i delete @ before pg_fetch_object i get an error: "Unable to jump to row 3 on PostgreSQL result index 4" I understand what's wrong and i know why is that @. What i do want to know is, if there is something wrong with this function or am i doing something wrong. I don't like that kind of errors. How can i stop before the end. In mysql there was no need to count rows. Thanks -- Uros
On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > Hi! > > I have some questions about coding in php with postgre. > > Here is my code > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > $row = 0; // postgres needs a row counter other dbs might not > while ($data = @pg_fetch_object ($qu, $row)) { > echo $data->username." ("; > echo $data->password ."): "; > echo $data->client_id."<BR>"; > $row++; > } > > When i execute this i get 3 records (in DB is also 3 records), if i > delete @ before pg_fetch_object i get an error: > > "Unable to jump to row 3 on PostgreSQL result index 4" > > I understand what's wrong and i know why is that @. > > What i do want to know is, if there is something wrong with this > function or am i doing something wrong. I don't like that kind of > errors. How can i stop before the end. for($i=0; $i < pg_numrows($qu); $i++) { $data = pg_fetch_object($qu, $row); echo $data->username." ("; echo $data->password ." )"; echo $data->client_id."<BR>>"; } You can blame the PHP people for having wildly dissimilar systems for iterating through result sets... > In mysql there was no need to count rows. Let's not start the MySQL vs. Postgres wars again... :) -- Adam Haberlach |A cat spends her life conflicted between a adam@newsnipple.com |deep, passionate, and profound desire for http://www.newsnipple.com |fish and an equally deep, passionate, and '88 EX500 |profound desire to avoid getting wet.
Hi I think You misunderstand the function You use. What was You expect to get when You try to access to not exsisting row? When You are doing "while" it will exit on ``false'' value - so when You access to not existing row the function produce ``false'', but also PHP produce Error message if You dont prevent it in php.ini or with "@" before function call. See that Yours code will exit "while" loop when You $row counter will get 4. First $row is set to 4 then "while" test the return value of function in $data. So don't be suprised such error warning. You are try to gen not existing object. If mysql relevant function dont produce error warning in such situation IMHO is something wrong with mysql access function in PHP rather than postgreSQL access function. But ... let's don't start wars again ;) BTW... IMHO it is not good practice to use errors as exit code for loops. Feel free to disagree ;)))) regards Robert > Hi! > > I have some questions about coding in php with postgre. > > Here is my code > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > $row = 0; // postgres needs a row counter other dbs might not > while ($data = @pg_fetch_object ($qu, $row)) { > echo $data->username." ("; > echo $data->password ."): "; > echo $data->client_id."<BR>"; > $row++; > } > > When i execute this i get 3 records (in DB is also 3 records), if i > delete @ before pg_fetch_object i get an error: > > "Unable to jump to row 3 on PostgreSQL result index 4" > > I understand what's wrong and i know why is that @. > > What i do want to know is, if there is something wrong with this > function or am i doing something wrong. I don't like that kind of > errors. How can i stop before the end. > > In mysql there was no need to count rows. > > Thanks >
Adam Haberlach wrote: > > On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > > Hi! > > > > I have some questions about coding in php with postgre. > > > > Here is my code > > > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > > $row = 0; // postgres needs a row counter other dbs might not > > while ($data = @pg_fetch_object ($qu, $row)) { > > echo $data->username." ("; > > echo $data->password ."): "; > > echo $data->client_id."<BR>"; > > $row++; > > } > > > > When i execute this i get 3 records (in DB is also 3 records), if i > > delete @ before pg_fetch_object i get an error: > > > > "Unable to jump to row 3 on PostgreSQL result index 4" > > > > I understand what's wrong and i know why is that @. > > > > What i do want to know is, if there is something wrong with this > > function or am i doing something wrong. I don't like that kind of > > errors. How can i stop before the end. > > for($i=0; $i < pg_numrows($qu); $i++) { As I understand the mechanism, a while loop, as in while ($data = @pg_fetch_object ($qu, $row)) { . . . would be faster than a for loop as above because with each iteration, PHP has to execute pg_numrows($qu), which, depending on how it is implemented (I don't know that and don't read C well enough to be able to take a peek at the source to figure it out), would require going through the entire result set to count the rows. Even if this only happens once at the first iteration (e.g. if PHP then caches the result), this could be a significant, unnecessary, overhead - particularly if the result set is large. With the while loop you simply avoid that. I don't see a problem with using the error as an exit condition for the loop, except that by switching off error reporting with @ you switch off _all_ errors, not only those that you know you'll get and which don't want to see, which can make debugging more difficult (but if you're debugging, you just remove the @ and add it again when you're done). Regards, Frank
On Fri, Jan 05, 2001 at 10:07:28PM -0800, some SMTP stream spewed forth: > On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > > Hi! > > > > I have some questions about coding in php with postgre. > > > > Here is my code > > > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > > $row = 0; // postgres needs a row counter other dbs might not > > while ($data = @pg_fetch_object ($qu, $row)) { > > echo $data->username." ("; > > echo $data->password ."): "; > > echo $data->client_id."<BR>"; > > $row++; > > } > > > > When i execute this i get 3 records (in DB is also 3 records), if i > > delete @ before pg_fetch_object i get an error: > > > > "Unable to jump to row 3 on PostgreSQL result index 4" > > > > I understand what's wrong and i know why is that @. > > > > What i do want to know is, if there is something wrong with this > > function or am i doing something wrong. I don't like that kind of > > errors. How can i stop before the end. > $nr=pg_numrows($qu); for($i=0; $i < $nr; $i++) This add another variable in exchange for saving an obscene amount of time counting rows on every iteration. The same situation occures when going through arrays, only with arrays if an array item is removed, then the number is different and may affect the row count and thus the loop. I doubt that such could happen with a resultset, but it is safer and a buttload faster to pre-count the rows. gh >>>>> for($i=0; $i < pg_numrows($qu); $i++) { > $data = pg_fetch_object($qu, $row); > echo $data->username." ("; > echo $data->password ." )"; > echo $data->client_id."<BR>>"; > } > > You can blame the PHP people for having wildly dissimilar systems > for iterating through result sets... > > > In mysql there was no need to count rows. > > Let's not start the MySQL vs. Postgres wars again... :) > > -- > Adam Haberlach |A cat spends her life conflicted between a > adam@newsnipple.com |deep, passionate, and profound desire for > http://www.newsnipple.com |fish and an equally deep, passionate, and > '88 EX500 |profound desire to avoid getting wet.
On Sat, Jan 06, 2001 at 11:22:07AM -0600, some SMTP stream spewed forth: > On Fri, Jan 05, 2001 at 10:07:28PM -0800, some SMTP stream spewed forth: > > On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > > > Hi! > > > > > > I have some questions about coding in php with postgre. > > > > > > Here is my code > > > > > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > > > $row = 0; // postgres needs a row counter other dbs might not > > > while ($data = @pg_fetch_object ($qu, $row)) { > > > echo $data->username." ("; > > > echo $data->password ."): "; > > > echo $data->client_id."<BR>"; > > > $row++; > > > } > > > > > > When i execute this i get 3 records (in DB is also 3 records), if i > > > delete @ before pg_fetch_object i get an error: > > > > > > "Unable to jump to row 3 on PostgreSQL result index 4" > > > > > > I understand what's wrong and i know why is that @. > > > > > > What i do want to know is, if there is something wrong with this > > > function or am i doing something wrong. I don't like that kind of > > > errors. How can i stop before the end. > > > $nr=pg_numrows($qu); > for($i=0; $i < $nr; $i++) > (And I wasn't even drunk at type-time.) (adds) > This add another variable in exchange for saving an obscene amount of > time counting rows on every iteration. > (occurs) > The same situation occures when going through arrays. > If an array item is removed, then the number is different and may affect > the row count and thus the loop. I doubt that such could happen with > a resultset, but it is safer and a buttload faster to pre-count the rows. > > gh > > >>>>> for($i=0; $i < pg_numrows($qu); $i++) { > > > $data = pg_fetch_object($qu, $row); > > echo $data->username." ("; > > echo $data->password ." )"; > > echo $data->client_id."<BR>>"; > > } > > > > You can blame the PHP people for having wildly dissimilar systems > > for iterating through result sets... > > > > > In mysql there was no need to count rows. > > > > Let's not start the MySQL vs. Postgres wars again... :) > > > > -- > > Adam Haberlach |A cat spends her life conflicted between a > > adam@newsnipple.com |deep, passionate, and profound desire for > > http://www.newsnipple.com |fish and an equally deep, passionate, and > > '88 EX500 |profound desire to avoid getting wet.
On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > Hi! > I have some questions about coding in php with postgre. > Here is my code > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > $row = 0; // postgres needs a row counter other dbs might not > while ($data = @pg_fetch_object ($qu, $row)) { > echo $data->username." ("; > echo $data->password ."): "; > echo $data->client_id."<BR>"; > $row++; > } > When i execute this i get 3 records (in DB is also 3 records), if i > delete @ before pg_fetch_object i get an error: > "Unable to jump to row 3 on PostgreSQL result index 4" > I understand what's wrong and i know why is that @. > What i do want to know is, if there is something wrong with this > function or am i doing something wrong. I don't like that kind of > errors. How can i stop before the end. > In mysql there was no need to count rows. there are nicer ways (look for one in the db-abstraction layer in PHP-LIB) but this works: > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); $number_of_rows = pg_numrows($qu); for ($counter=0;$counter<$number_of_rows;$counter++) { $data_array = pg_fetch_array($qu, $counter); echo "$data_array[username]"; } z.
On Sat, Jan 06, 2001 at 11:52:51AM -0600, GH wrote: > On Sat, Jan 06, 2001 at 11:22:07AM -0600, some SMTP stream spewed forth: > > On Fri, Jan 05, 2001 at 10:07:28PM -0800, some SMTP stream spewed forth: > > > On Fri, Jan 05, 2001 at 11:17:29PM +0100, Uro Gruber wrote: > > > > Hi! > > > > > > > > I have some questions about coding in php with postgre. > > > > > > > > Here is my code > > > > > > > > $qu = pg_exec ($db_conn, "SELECT * FROM clients ORDER BY username"); > > > > $row = 0; // postgres needs a row counter other dbs might not > > > > while ($data = @pg_fetch_object ($qu, $row)) { > > > > echo $data->username." ("; > > > > echo $data->password ."): "; > > > > echo $data->client_id."<BR>"; > > > > $row++; > > > > } > > > > > > > > When i execute this i get 3 records (in DB is also 3 records), if i > > > > delete @ before pg_fetch_object i get an error: > > > > > > > > "Unable to jump to row 3 on PostgreSQL result index 4" > > > > > > > > I understand what's wrong and i know why is that @. > > > > > > > > What i do want to know is, if there is something wrong with this > > > > function or am i doing something wrong. I don't like that kind of > > > > errors. How can i stop before the end. > > > > > $nr=pg_numrows($qu); > > for($i=0; $i < $nr; $i++) > > > (And I wasn't even drunk at type-time.) > (adds) > > This add another variable in exchange for saving an obscene amount of > > time counting rows on every iteration. > > I've always assumed that the number of rows is known by the result set so pg_numrows() is merely a (arguably) trivial lookup. Now that I've looked at the code, that assumption looks correct. The overhead seems to be one PHP function call and two C function calls. No counting, no loops, no obscenities. Unless PQntuples() is really stupid, but I'm not going to wander through another source tree tonight. -- Adam Haberlach |A cat spends her life conflicted between a adam@newsnipple.com |deep, passionate, and profound desire for http://www.newsnipple.com |fish and an equally deep, passionate, and '88 EX500 |profound desire to avoid getting wet.
On Sat, Jan 06, 2001 at 05:12:27PM +0100, Frank Joerdens wrote: > > > I understand what's wrong and i know why is that @. > > > > > > What i do want to know is, if there is something wrong with this > > > function or am i doing something wrong. I don't like that kind of > > > errors. How can i stop before the end. > > > > for($i=0; $i < pg_numrows($qu); $i++) { > > As I understand the mechanism, a while loop, as in > > while ($data = @pg_fetch_object ($qu, $row)) { . . . > > would be faster than a for loop as above because with each iteration, PHP has to execute > pg_numrows($qu), which, depending on how it is implemented (I don't know that and don't > read C well enough to be able to take a peek at the source to figure it out), would > require going through the entire result set to count the rows. Even if this only happens > once at the first iteration (e.g. if PHP then caches the result), this could be a > significant, unnecessary, overhead - particularly if the result set is large. With the > while loop you simply avoid that. I don't see a problem with using the error as an exit > condition for the loop, except that by switching off error reporting with @ you switch off > _all_ errors, not only those that you know you'll get and which don't want to see, which > can make debugging more difficult (but if you're debugging, you just remove the @ and add > it again when you're done). Once again, this is probably all due to a difference between MySQL and Postgres. Judging by the MySQL code, there is a provision for the client-side libraries to pass tuples on to the application in the order they are sorted without necessarily retrieving them all to the client. AFAIK, Postgres does not do this unless you specifically use cursors to pull down a window of data at a time (this is correct behavior IMHO, your feelings may vary). I assume that this causes the MySQL client libraries to let you exec a query and then pull rows out of it until it hits the end, at which point it indicates that you are done. PHP carries this model through to the PHP side of things. Since Postgres pulls down the entire result set, they are available for random access. This shows in the PG libraries and that behavior, as well, has been carried over to PHP. Since PHP has no unified method for database access, you notice these differences. (and judging by the number of times I've mentioned PHP and MySQL in this post, it is time for this thread to go elsewhere. Ask the MySQL people to let you do random access of a large data set or as the PHP people to unify their database acess model). -- Adam Haberlach |A cat spends her life conflicted between a adam@newsnipple.com |deep, passionate, and profound desire for http://www.newsnipple.com |fish and an equally deep, passionate, and '88 EX500 |profound desire to avoid getting wet.
Adam Haberlach wrote: [ . . . ] > Once again, this is probably all due to a difference between MySQL and Postgres. > Judging by the MySQL code, there is a provision for the client-side libraries to > pass tuples on to the application in the order they are sorted without necessarily > retrieving them all to the client. AFAIK, Postgres does not do this unless you > specifically use cursors to pull down a window of data at a time (this is correct > behavior IMHO, your feelings may vary). Oops, this means that I have been coding on a false assumption for quite a while now. I guess I'll be changing quite a few while loops into for loops this week. But it's good to have a conclusive verdict on the issue. I'd been feeling somewhat uneasy about it all allong, what with not knowing exactly what the mechanism was . . . thanks for making this clear! [ . . . ] > (and judging by the number of times I've mentioned PHP and MySQL in this > post, it is time for this thread to go elsewhere. I've cross-posted to php-pgsql where I think it belongs, actually. Regards, Frank
Absolutely! I also have Bruce Momjian, 'PostgreSQL' and Worlsey & Drake, 'Practical PostgreSQL'. All three have slightly different approachs and things not found in either of the others. That's why I almost ordered Conni's.... For a beginner, I would recommend all three, starting with yours for the installation information; Apache, PHP, and PostgreSQL. For example, Bruce begins by saying, "I assume that you have a test database running" ... in my case that was a bad assumption. :) Thanks, Warren >From: Hans-J�rgen Sch�nig <hs@cybertec.at> >To: warrenmassengill@hotmail.com >Subject: PHP and PostgreSQL >Date: Fri, 11 Oct 2002 08:34:43 +0200 > >I hope you like our book :) > > Hans > >-- >*Cybertec Geschwinde u Schoenig* >Ludo-Hartmannplatz 1/14, A-1160 Vienna, Austria >Tel: +43/1/913 68 09; +43/664/233 90 75 >www.postgresql.at <http://www.postgresql.at>, cluster.postgresql.at ><http://cluster.postgresql.at>, www.cybertec.at <http://www.cybertec.at>, >kernel.cybertec.at <http://kernel.cybertec.at> _________________________________________________________________ Chat with friends online, try MSN Messenger: http://messenger.msn.com