veledrom 32 Master Poster

Hi,

I need to find a correct approach to do database process. Please have a lot at my code below, I am sure it is not good practise. I'll be happy if you can help me avoiding many connections to mysql db once there is a connection already established. Also, can I move mysql_ping() into inc.db.php?

Thanks in advance

inc.db.php

<?php
function connectDB(){
	$host = "localhost";
	$uid = "root";
	$psw = "";
	$db = "test";
		
	$hostConn = @mysql_connect($host, $uid, $psw);

	if($hostConn){
		$dbConn = mysql_select_db($db);
		if(! $dbConn){
			return false;
		} else {
			return true;
		}
	} else {
		return false;
	}
}
?>

index.php

<?php
require_once "inc.db.php";


if(@mysql_ping($hostConn)){
	echo "DB Conn already set<br />";
} else {
	echo "DB Conn not set<br />";
	if(connectDB() !== true)
		echo "DB Conn error";
		exit;
	}
}

$result1 = mysql_query("INSERT INTO table VALUES ('xxx')");
mysql_free_result($result1);

echo "Thanks";
?>
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.