Php – thesql connection

database connectionMySQLPHP

Im using this code for mysql connection

$con = mysql_connect("localhost:/var/lib/mysql/mysql.sock", "abc" , "xyz");
if (!$con)
{
    die('Could not connect: ');
}
mysql_select_db("database_name", $con);

Im including this file on the top of every php file that im using for parsing and then storing the data in database.Now when the original site gets down then my parsing files starts giving error
"Warning: mysql_connect(): Too many connections in /home/clickindia/public_html/appuonline/db.php on line 2
Could not connect:"

What should i do to save my site from these errors.Do you think that i should use mysql_close($con); But how and where??? Im using die in my code is that correct or should i use close etc. plz help!!

Best Answer

You say you put this connection code "on the top of every php file".

My question is: do you use several of these files for a single page?

If this is the case, then you should open the connection only once for a page. This could be the explanation of the error.

Artem's advises are pertinent too.

Related Topic