Hi,

I want to re-use already estanbished DB connection but I don't know know how to do it. Please help me to modify my code below.

Thanks in advance

function connectDB(){
	$host = "localhost";
	$uid = "root";
	$psw = "";
	$db = "mydb";

	
	$hostc = @mysql_connect($host, $uid, $psw);

	if($hostc){
		$dbc = mysql_select_db($db);
		if(! $dbc){
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}

$result = connectDB();

if($result === true){
	echo "OK";
} else {
	echo "NO";
}
<?php
$connection1 = mysql_connect("hostname","username","password",true);
$connection2 = mysql_connect("hostname","username","password",true);

$db_selected1 = mysql_select_db('db1', $connection1);
$db_selected2 = mysql_select_db('db1', $connection2);
?>

It will create a new connection without disturbing the old connection.
If you face any problem regarding this again inform me.
Good luck.

I don't want to establish another connection when I call function above. If I already called it then it should use previous one to avoid connecting to mysql second time.

Excellent. Thanks mahavir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.