Re: select colum in array.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Richard Lynch wrote:
On Thu, August 17, 2006 9:02 am, João Cândido de Souza Neto wrote:
I´m not sure if it´s the right place to get such answer, but if
someone
know, please, help me.

In a select id,name,picture1,picture2,picture3 from product where
id="10" i
get an array with each colum in each element like this $result ("id"
=>
"10", "name" => "name of product", "picture1" => "pic1.gif",
"picture2" =>
"pic2.gif", "picture3" => "pic3.gif").

Is there any way in select to get something like this:

$result ("id" => "10", "name" => "name of product", "pictures" =>
array(
"pic1" => "pic1.gif", "pic2" => "pic2.gif", "pic3" => "pic3.gif") ).

If your database (MySQL? PostgreSQL? SQL Server? Oracle? Sybase?
Informix? DB?) has an array datatype, or, I guess, any datatype that
gets mapped to an array by the database-client + PHP implementation,
then, in theory, you could compose a SELECT that would "work" for that
particular database...

You'd be better off, however, to munge the data in your PHP, in my
opinion as that will be much more portable, and unless you are getting
thousands of rows at once, not much slower.

These functions will be very useful:
http://php.net/array_slice
http://php.net/array_splice

I think this is correct for your example:
$pictures = array_slice($result, 3);
$results = array_splice($result, 3, 3, $pictures);

The first line grabs everyting from the 3rd element on (the 3 pictures)

The second line replaces everything from element 3 through 6 (the
second 3 is a length parameter) with your new array.

I must say, I'm of the opinion that as much data processing should be done in the database server possible. For one thing, munging data in PHP means you MUST have a complete copy of the result set in memory, instead of processing it row by row. PHP's 8MB memory limit can sometimes pose a problem if that munging involves several copies of the data floating around.

Further, database servers are, in my opinion, easier to distribute than web servers if load is an issue. While your single script could easily connect properly to a database cluster, distributing the PHP scripts involves somehow getting the client to the right one, either by roundrobin DNS, or some sort of redirection mechanism.

I also tend to think that the database would be faster. Perhaps the PHP script would only a few milliseconds longer than the database to process the data, but when you have multiple requests per second, those few milliseconds can REALLY add up. I'd rather spend the processor time on the webserver gzipping the data as it goes out. Of course, I have no benchmarks telling me that PHP would be slower than a database server for messing with data, I'm just making an assumption.

Regards, Adam.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux