Connection Timeout: How to speed up my mySql connection?, Is this the correct way

Is this the correct way to open and close the mysqlConnection, because i need to proccess thousands and thousands of records. sometimes server return connection timeout. eventhough i have increased timout period. please give me some help tip. tks in advance

$dbObject=new MySQLConnectionFactory ;
mysql_query(“SET character_set_client=utf8”, $dbObject->create());
mysql_query(“SET character_set_connection=utf8”, $dbObject->create());

$sql=“SELECT e.cname as customer, m.cname as manger, e.ccode as ccode
FROM tblcust e, tblcust m
WHERE e.mcode=m.ccode and e.mcode=‘$strCCode’
order by e.cname”;

$result = mysql_query($sql, $dbObject->create());
while($row = mysql_fetch_assoc($result)) {
<Here i call the following function and retrive somthing>
}
mysql_free_result($result);
mysql_close($dbObject->create());

function funTrial($StrCode,$strDte){
$dbObj=new MySQLConnectionFactory;
$triSql =“SELECT count(pagerno) as tri
FROM tbldetails s1
WHERE s1.ccode = ‘$StrCode’
AND s1.col1 <> ‘invalid’
AND s1.col5 = ‘3’
AND s1.col6 <=‘$strDte’
AND s1.col6=(select MAX(s2.col6) FROM tbldetails s2
WHERE s1.pagerno = s2.pagerno AND s2.ccode = ‘$StrCode’
AND s2.col1 <> ‘invalid’ AND s1.col5 = ‘3’ AND s2.col6 <=‘$strDte’)”;

$triResult = mysql_query($triSql, $dbObj-&gt;create());
$triRow = mysql_fetch_assoc($triResult);
$Trial = $triRow['tri'];
mysql_free_result($triResult);
mysql_close($dbObj-&gt;create());	
return $Trial;	

}

function funTerm($StrCode,$strDte){
$dbObj=new MySQLConnectionFactory;
$terSql =“SELECT count(pagerno) as ter
FROM tbldetails s1
WHERE s1.ccode = ‘$StrCode’
AND s1.col1 <> ‘invalid’
AND s1.col5 = ‘2’
AND s1.col6 <=‘$strDte’
AND s1.col6=(select MAX(s2.col6) FROM tbldetails s2
WHERE s1.pagerno = s2.pagerno AND s2.ccode = ‘$StrCode’
AND s2.col1 <> ‘invalid’ AND s1.col5 = ‘2’ AND s2.col6 <=‘$strDte’)”;

$terResult = mysql_query($terSql, $dbObj-&gt;create());
$terRow = mysql_fetch_assoc($terResult);
$Terminate = $terRow['ter'];
mysql_free_result($terResult);
mysql_close($dbObj-&gt;create());	
return $Terminate;

}

If your goal is to open and close the connection many times in the script, why not use regular connections instead of a pool of persistent connections?

Does $dbObj->create() return the same connection each time you call it? Odd name if it does. Either way, why not store the connection in a variable instead of calling create() every time you reference the connection?

Create() is the public function in the Global.inc(Connection pooling), lets say if i close and open mysql connection very often. will it cause any effects when mysql server return results. i have carefully close all the recordset and connection every time after i retrieve…

<?php

class MySQLConnectionFactory {
static $SERVERS = array(
array(
‘host’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ‘triadpass’,
‘database’ => ‘pagerdb1’),
);

public static function create() {
// Figure out which connections are open, automatically opening any connections
// which are failed or not yet opened but can be (re)established.
$cons = array();
for ($i = 0, $n = count(MySQLConnectionFactory::$SERVERS); $i < $n; $i++) {
$server = MySQLConnectionFactory::$SERVERS[$i];
$con = mysql_pconnect($server[‘host’], $server[‘username’], $server[‘password’]);
if (!($con === false)) {
if (mysql_select_db($server[‘database’], $con) === false) {
echo('Could not select database: ’ . mysql_error());
continue;
}
$cons = $con;
}
}
// If no servers are responding, throw an exception.
if (count($cons) == 0) {
throw new Exception
('Unable to connect to any database servers - last error: ’ . mysql_error());
}
// Pick a random connection from the list of live connections.
$serverIdx = rand(0, count($cons)-1);
$con = $cons[$serverIdx];
// Return the connection.
return $con;
}
}
?>

Try:

set_time_limit(0);

Still time out…dont know wat to do, still scratching…It is not dat i want to close and open my connectin very often, for example every customer has his own history of data. out this data i need to use customer as search string and take all his history and manuplate a report for him. so my goal is not about open and close connection, GIVE ME A SUGGESTION ON THIS, METHOD 1, OPEN MYSQL CONNECTION ONE TIME AT THE BEGINING OF THE PROCESS AND CLOSE AT THE END. METHOD 2, CONNECTION POOLING OPEN AND CLOSE IMMEDIATLY AFTER AND BEFORE EVERY DATA MANUPULATION. WHICH IS THE BEST WAY AND FASTEST?

Start an instance of your database object and aggregate it around to whatever code needs it. Some pages of some sites might well access their database a couple of hundred times, do you really want the overhead of opening and closing database connections when it is better for a page to use a single connection?