!Need advice from other PHP developers.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Many years ago when I first learnt abt web dev in school,

I was taught this methodology:

[Open connection]

blah blabh

[execute sql]
[loop thru recordset]

    <table>[data] </table>

[recordset close]
[end loop]


[Close connection]

Over the peroid of time, I noticed that the above method may cause  
connection problems on heavy sites.

Recently, I have use another method to retrieve data from server

[Open Connection]
[declare 2D array]
[loop thru recordset]
[2D array] = [data]
[end loop]
[Close connection]


[loop thru the 2D array]
<table>[2d array's DATA] </table>
[end array loop]


It performs better as all the data retrieveal and processing are all  
processed at the beginning of the page.

Need comments and advise please.

1. Are there better ways to manage connections and easier data retrieval to  
ensure performance of the site under heavy load?

2. Are there any potential pitfalls regarding my 2nd method of data  


 Posted Via Usenet.com Premium Usenet Newsgroup Services

Re: !Need advice from other PHP developers.

Eric Layman wrote:

Quoted text here. Click to load it


That is how I retrieve data too, via ADODB (PHP) GetArray().
Nothing wrong with it. It even offers you handy last minute scrubbing over  
an array instead of awkward recordsets.

Quoted text here. Click to load it

PHP recycles its connection behind your back.
If scriptA.php connects to a database, a connection is build based on the  
login-credentials (host, database, username, password, etc).

If scriptB.php, scriptC.php, etc, use the same logincredentials, which is  
almost always the case in webdriven databases, you'll find that they do NOT  
need the connection/authentication overhead that scriptA.pp needed.
PHP remembers the logincredentials, DOESN'T CLOSE ITS CONNECTION, and  
recycles the connection when ANY script uses the same logincredentials.
So PHP is doing things right performancewise.
I don't think you'll gain a lot of performanceimprovement by using another  

Quoted text here. Click to load it

Just be aware how your elements in your array represent 'strange values'  
coming from the database, like NULL. Are they stored as empty string? Or  
are they really (PHP)NULL? Make sure you check and know.

Quoted text here. Click to load it

Erwin Moller

Re: !Need advice from other PHP developers.

Erwin Moller wrote:

Quoted text here. Click to load it

That depends on which connect function you use, *_connect or *_pconnect.

Quoted text here. Click to load it

If you use *_connect, the connection is closed when the script has been  
executed, even if you don't use *_close, while *_pconnect leaves the  
connection alive.

Quoted text here. Click to load it

And don't get too much data, it's easier this way to hit the PHP max RAM size,  
specially if you throw the data into functions in bad ways and you may have 2+  
copies of the original array.



Re: !Need advice from other PHP developers.

Eric Layman wrote:

Quoted text here. Click to load it

After this, all your data is now stored in $2dArray, using up memory. If
you loop through the recordset, reading one line at a time and outputting,
you use less memory. For small bits of data, that's unlikely to be a
problem though.

By the way, you may want to put more consideration into your message
subject lines in future. "!Need advice from other PHP developers." could
easily apply to 90% of the messages to this group. A more specific subject
line which may help clue readers in to what your post will be about would
have been "Caching SQL results into an array" or somesuch.

Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Re: !Need advice from other PHP developers.

Eric Layman wrote:
Quoted text here. Click to load it


This will help free up MySQL resources with heavily used sites, but will  
require more memory in PHP to hold your data.

For a single thread, things will run more slowly due to the extra  
processing required to buffer the data in an array.

For the entire system, things may run more quickly or more slowly - it  
all depends on where your constraints are.  Personally, I can't see that  
it will really help that much other than releasing the connection a  
slight bit more quickly.  And if you're that connection-constrained, the  
number of MySQL connections is probably too low.

Personally, I don't do any extra buffering.  But I don't request  
resources before I need them, and I release resources as soon as I'm  
through with them.  That means, for instance, I don't do a SELECT until  
I'm ready to use the data, and I close the result set as soon as I'm  
through with the data.  I also close the connection as soon as I'm done  
with all MySQL requests.

But you claim this is faster - I'd like to see some real benchmarks on  
this.  I really suspect the increase in speed is more a matter of  
perception (quite easy to do).

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: !Need advice from other PHP developers.

Quoted text here. Click to load it

If you can read say tens or hundreds of records then the first method is  
better. Not oveload server memory and is quickly.
But if you can read thousand or more records then is better to use other  
[open connection]
[open temporary file]
[loop thru recordset]
    [write to file]
[end loop]
[close connection]
[close file]
[open file]
[loop thru lines]
    [print to html]
[end loop]
[close file]

By my experience all servers have less RAM then disk space ;-) and read or  
write text file is a simple task for server.


Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail  
from another non-spammer site please.)

Site Timeline