424,294 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,294 IT Pros & Developers. It's quick & easy.

!Need advice from other PHP developers.

P: n/a
Hi,

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

I was taught this methodology:

[Open connection]

<html>
blah blabh

[execute sql]
[loop thru recordset]

<table>[data] </table>

[recordset close]
[end loop]

<html>

[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]
[recordset.close]
[end loop]
[Close connection]

<html>

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

</html>

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
retrieval?

Thanks


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Feb 5 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Eric Layman wrote:
Hi,

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

I was taught this methodology:

[Open connection]

<html>
blah blabh

[execute sql]
[loop thru recordset]

<table>[data] </table>

[recordset close]
[end loop]

<html>

[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]
[recordset.close]
[end loop]
[Close connection]

<html>

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

</html>

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

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.
>
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?
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
approach.

>
2. Are there any potential pitfalls regarding my 2nd method of data
retrieval?
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.
>
Thanks


Regards,
Erwin Moller
Feb 5 '07 #2

P: n/a
Eric Layman wrote:
[Open Connection]
[declare 2D array]
[loop thru recordset]
[2D array] = [data]
[recordset.close]
[end loop]
[Close connection]
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!
Feb 5 '07 #3

P: n/a
Erwin Moller wrote:
>1. Are there better ways to manage connections and easier data retrieval
to ensure performance of the site under heavy load?

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.
That depends on which connect function you use, *_connect or *_pconnect.

PHP remembers the logincredentials, DOESN'T CLOSE ITS CONNECTION, and
recycles the connection when ANY script uses the same logincredentials.
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.

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

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.
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.

--

//Aho
Feb 5 '07 #4

P: n/a
Eric Layman wrote:
Hi,

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

I was taught this methodology:

[Open connection]

<html>
blah blabh

[execute sql]
[loop thru recordset]

<table>[data] </table>

[recordset close]
[end loop]

<html>

[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]
[recordset.close]
[end loop]
[Close connection]

<html>

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

</html>

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
retrieval?

Thanks


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Eric,

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.
js*******@attglobal.net
==================
Feb 5 '07 #5

P: n/a
"Eric Layman" <erricson@layspíše v diskusním příspěvku
news:11*************@sp6iad.superfeed.net...
Hi,

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

I was taught this methodology:

[Open connection]
<html>
blah blabh
[execute sql]
[loop thru recordset]
<table>[data] </table>
[recordset close]
[end loop]
<html>
[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]
[recordset.close]
[end loop]
[Close connection]
<html>
[loop thru the 2D array]
<table>[2d array's DATA] </table>
[end array loop]
</html>
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
methods.
[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.)
Feb 6 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.