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