|
Posted by sam.s.kong on 09/27/68 11:37
Pedro Graca wrote:
> sam.s.kong@gmail.com wrote:
> > 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
>
> You can easily access the returned resource as a kind of array with
>
> $result = mysql_query("select * from table1");
> $var = mysql_result($result, 3, 2);
>
> If you choose to put all returned data into a "proper" array, don't
> forget to
>
> mysql_free_result($result);
Thanks.
This is the answer I needed.
The reason I want to dump the recordset to an array is to avoid
cleaning up (disconnection) so that I can forget about db-related jobs.
function get_data($sql){
//connect to dbms
//select db
//select data and dump to an array
//close db (free db resource)
}
$data = get_data("select * from table1");
I'll make the function in a library file, and just call the function
and forget about connecting/disconnecting/freeing db resources.
One benefit is that I won't make mistake to forget about cleaning up.
Array will be automatically GCed, right?
To summarize, my goals are:
1. Put db-related routines in one function (or class) to avoid
repeating same codes.
2. Avoid mistakes like forgetting mysql_free_result
3. You can improve the function later and it will affect everywhere.
4. Make db-accessing code simple. (Just throw an sql and get the
result)
What do you think?
Sam
Navigation:
[Reply to this message]
|