views:

22

answers:

2

I have ajax call which pulls from the processing script 'getajax.php'.

Call is made to 'getajax.php' script which has the db connection details, select, functions, etc.

My question is:

Everytime a call is received by 'getajax.php' it will go through mysql_connect, mysql_select, then queries.

Is this the correct aproach to handle thousands of simultaneous calls?

How can I avoid mysql connection to be opened everytime a call is made, reusing one existing connection for all calls.

Trying to have one call to:

$dbconnect = mysql_connect('host','user','pass');
mysql_select_db('databasename') or die( "Unable to select database");

How can I open a persistent connection on parent so 'getajax.php' script just reususes this connection without running these mysql commands over and over.

Unsure how to aproach.

Thanks All!

A: 

It sounds like you need connection pooling, where a set of connections is always maintained for clients. It reduces the overhead of opening a new connection. You would normally not have a connection per client, but a set of connections configured for a number of simultaneously requesting clients.

See here for more details on mysql_pconnect and here for a related SO question.

Brian Agnew
A: 

You can use mysql_pconnect (http://www.php.net/manual/en/function.mysql-pconnect.php) which creates a persistent connection to the database.

halfdan
Thanks so much. ! Works like charm.
Codex73
No! But this is not necessary, pconnect help you to minimize the overhead you'd have if you created a new connection to the DB every time. mysql_select_db doesn't produce any noticeable overhead. You still need to have pconnect/select_db every time your script is being executed.
halfdan
Super! I also saw on the manual link that we can include the select as part of query thus eliminating the function call. Something like this: mysql_query('SELECT * FROM database_name.table_name'); http://php.net/manual/en/function.mysql-select-db.php
Codex73
Of course that works, but if you have a lot of queries you might not want to prepend the database name in the query as it might get very ugly to handle if your database name changes (new install e.g.).
halfdan
Great Thanks!!!
Codex73