You are here: Re: Is this a good idea? « PHP Programming Language « IT news, forums, messages
Re: Is this a good idea?

Posted by sam.s.kong on 01/18/06 21:57

Iván Sánchez Ortega wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Chung Leong wrote:
>
> > R. Rajesh Jeba Anbiah wrote:
> >>
> >> As many people have pointed out, *never* dump the table data into
> >> array. Fetch the record and immediately get that processed. If you have
> >> any *valid* reason, buffer the data into a very very small (known)
> >> sized array. If using MySQL, use the LIMIT if possible.
> >
> > The reasoning being?
>
> Code refactoring, for example.
>
> As you may already know, refactoring is almost always a good idea, as it
> reduces complexity of the algorithm, procesing time, and increases the
> cache hit ratio, to name a few consecuences.
>
> Let's suppose the following example:
>
> <?php
>
> mysql_pconnect(blahblahblah);
> $r = mysql_query(blahblahblah);
> $db_results = array();
> while ($row = mysql_fetch_array($r))
> {
> $db_results[] = $row;
> }
>
> foreach ($db_results as $row)
> {
> foobar;
> }
>
> ?>
>
> Well, let's refactor that code:
>
>
> <?php
>
> mysql_pconnect(blahblahblah);
> $r = mysql_query(blahblahblah);
> while ($row = mysql_fetch_array($r))
> {
> foobar;
> }
>
> ?>
>
>
> Less complexity, less CPU time, less memory, less code. Any developer that
> has been taught anything about algorithms knows that. You'd better have a
> good reason to not refactorize your code in this way.

Well, the example is not good for the issue here.
The following code will be encapsulated.

> mysql_pconnect(blahblahblah);
> $r = mysql_query(blahblahblah);
> $db_results = array();
> while ($row = mysql_fetch_array($r))
> {
> $db_results[] = $row;
> }

So when the practical code will be:

$result = get_result("select * from table1" [, "myDB"]);
for($result as $row){
foobar();
}

Of course, it has overhead, but that's more modular and most modularity
comes with costs.

Now I understand your point and I think dumping db data to an array
should be carefully used.

Thanks.
Sam

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация