tags:

views:

85

answers:

3

Hello, I am making a simple blog for myself and while reading through the PHP manual, I found this http://us2.php.net/manual/en/function.pg-query.php

It says

resource pg_query ([ resource $connection ], string $query )

...

Note: Although connection can be omitted, it is not recommended, since it can be the cause of hard to find bugs in scripts.

Why is it not ok to just use the last connection? I never plan on having more than 1 connection open per PHP script, so how would this ever cause bugs for me?

+1  A: 

This is to accommodate those who need explicit calls to various databases. If you don't, ignore it :) Some scripts work on local and remote databases, and yet others work on multiple local or multiple remote databases.

Jonathan Sampson
Or you could just create a database class that queried stuff for you, save the connection in a variable, and passed it in. Takes like 1 minute to write and you never have to think about it again :)
Chacha102
It might just be me, but I think that being more explicit is suppose to help debugging, as it doesn't require me to know the 'magic helper' functionality.
Chacha102
@ChaCha, yes I would agree generally here, but for small applications an extra parameter to pass around seems a bit verbose..
Earlz
@Chacha102: You're obviously not a Rails person. ;)
musicfreak
Meh. I tend to use the same database classes for large projects as I do small projects. Build it right once, shouldn't really have to rebuild it again.
Chacha102
@musicfreak Nope. PHP/JS person. I am starting to learn Perl however. That will be interesting.
Chacha102
A: 

Perhaps it is due to the order of arguments? If it was regarding a recommendation to explicitly use a connection resource, the same note would be on mysql_query(). Unless there is something specific to PostgreSQL I am unaware of.

In short, I don't see any problem omitting the connection argument for single connection applications.

enbuyukfener
+13  A: 

Hah. "I never plan on having more than 1 connection open per PHP script."

I remember the last time I said that. It was back in 'ought three. I was a young whippersnapper then, much like yourself. Full of spit and vinegar. Why do something if I don't have to? That was the prevailing wisdom in our little dot-com startup. "Just get it done!" we'd shout. Also, we wore onions on our belts.

Well... time came that I added a quick little statistics database in to the main site. Nothing special, just wanted some stats tracked separately. I figured I'd re-use the database wrapper. It was a good wrapper for it's time! Abstracted out all the database functions I'd need. But as soon as I added it in there, some wacky things started happening. It didn't make sense. I had two separate database wrapper objects... two separate connections! How could they affect each other? But then users would be logged out randomly. Sessions would fail. Sometimes a key update would go bad. Some queries ran on the wrong databases. Dogs and cats started living together! It was mass hysteria!

If only I had specified that connector originally. If only I had kept them specific, so pg_query would know which one to use. So much data loss could have been prevented. So many good tuples... so much good data. Lost. Lost...

*sniff *

zombat
It was trivial(<100 lines of php code still) to change all my stuff to use explicit queries.. so I guess thinking ahead FTW
Earlz
Definitely a good idea :) If you ever add another database connection somewhere and start alternating between them, you really need to have the connection parameter specified. Otherwise it's like walking up hill both ways. In the snow!
zombat
Thanks for the entertainment zombat.
jeerose
Get off my lawn! Back in my day, we didn't have "entertainment"... we had Mosaic.
zombat
Agree, specify the database connection is a MUST becose that's how the good code is wrote. Even if youre actually working with only 1 db, why take the risk to have to edit all your code in the future? For gain, how much, 3 seconds while first-writing your code?
DaNieL