467,923 Members | 1,331 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

MySQL connection, when to close, when to connect...

Hi,

I am trying to tidy up my code and now i am looking at my database work.
Now correct me if i am wrong, (I am using mysql_connect).

Lets assume a few functions...

//////////////////////////////////
// code
/////////////////////////////////
function A()
{
// connect
// run a SELECT query
// close
}

function B()
{
// connect
// run a SELECT query
}

Now according to the documentation A() and B() are the same because the
database is closed at the end of the function.
So in function B() the close in implied. Right?

But now lets assume within another function that i do many selects

function testDB()
{
B();
B();
B();
B();
// close the id
}
now surely connecting and closing all the time is very bad for the
performances.

So should i save the connection ID to make sure that the DB only connects if
it is not open?
Something like...

///////////////////
// code
//////////////////

$link = 0;
function A()
{
global $link;
if( $link == 0){
$link = // connect
}
// run a SELECT query
}

The case above would be OK if the function does not close the connection
when it exits?
Should i rather do

// some php
$link = // connect;
function A()
{
global $link;
// if not connected then big problem...
// run a SELECT query
}

function CloseA()
{
// close $link;
}

What would be the best way to do it?
Many thanks

Sims
Jul 17 '05 #1
  • viewed: 1876
Share:
2 Replies
Seb
Hi,

According to me, the connexion is not closed at the end of the function,
but at the end of the script.

I'm using Oracle, but I don't think it's very different.

I've built an abstraction class to manage the database connections and
queries.

I use one method to open the connection and instantiate my objects (that
is my connexions), then other methods to perform queries.

That gives :

Connect = new DB();
Connect->select(....);

With Oracle we also have persistant connections using pconnect : PHP
manage database connexions and don't close them, they are re-used when
needed. I don't know if it exists with MySQL.

Of course opening a new connection for each request in the same script
is really not good ;-). I wouldn't be your database server when traffic
will increase on your site ;-)

Seb
Jul 17 '05 #2
MySQL does support persitent connections.

I have an abstraction layer written myself as well (to support multiple
databases). I generally pass another variable indicating if a persistent
connection is needed. That way, you can choose (when you create the object)
to have persistent connections in your script or not.
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Josh | last post: by
reply views Thread by Mike Chirico | last post: by
reply views Thread by Daniel Crespo | last post: by
3 posts views Thread by yes_its_just_me | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.