|
Posted by sam.s.kong on 01/18/06 19:49
sam.s.kong@gmail.com wrote:
> Hi!
>
> I've been programming ASP for 5 years and am now learning PHP.
> In ASP, you can use GetRows function which returns 2 by 2 array of
> Recordset.
> Actually, it's a recommended way in ASP when you access DB as it
> disconnects the DB earlier.
> Also, it's handy as you can directly access any data in the array
> without looping.
>
> As far as I know, there's no such function in PHP and I can make one.
> My question is whether it's good in PHP.
>
> pseudo-code:
>
> $data = get_data("select * from table1");
> $var = $data[3][2]; //value at 4th row, 3rd column
>
> This way, I can wrap db connection, data retrieval, and error handling
> with one function (or maybe a class).
> Is the idea workable?
So far, it doesn't sound good.
Most people here are against it.
In ASP, releasing resourcesearly is very important, especially with
database.
If you're interested, you may read
http://www.learnasp.com/advice/whygetrows.asp .
In ASP, I abstracted db retrieval in a class and closed DB connection
early in the class and left data in memory as 2d array format.
ASP(VBScript) uses reference counting to dispose memory and the array
is guaranteed to be removed as soon as it goes out of scope.
Of course, the class is implemented in a library file and each page
that includes it doesn't have to worry about resources.
If I just let each page loop each record, some pages might forget
releasing db resources.
And I feel uncomfortable with the idea that db is opened at the top of
a page and closed somewhere else far from the top.
The problem gets worse if a pages includes several pages and the
includes pages access database and they don't release db resources as
data are used in the including page.
It's ok that I lose some performance and use a little bit more memory
when I abstract data access.
But if that causes trouble severely, I must think about it again.
Can somebody direct me to a best practice that's from very reliable
source or PHP official site?
Thanks.
Sam
[Back to original message]
|