Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

How to Connect to mysql database with php?

before any operations are to be made on the data, you have to establish a connection to the DB. There is Two types of connection are exist , normal connection and persistent connection

persistent connection doesn’t differ from usual connection, except that different function is used for connection establishment, if you close the persistent connection it will not effect.

There is no much difference in b/w the two connection types , in normal single-time MySQL connections are created, and closed once script execution is completed.This is good for rare connections, when PHP page isn’t requested too frequently.

In persistent connections type it will keep connection open for some particular time, and if the page loads multiple times, PHP code will reclaim the same connection.

Establishing connection

we Use the PHP mysql_connect() function to open a new connection to the MySQL Database.
we use the PHP mysql_pconnect() function to open a new persiste connection to the MySQl Database.

Both the function has same parameter , both used exactly in same way.

// normal Connection
mysql_connect('host_name','user_name','password');
// persistent Connection
mysql_pconnect('host_name','user_name','password');  

Selecting mysql database

mysql_select_db('databasename',$conn); 
// select mysql database

here $conn is an link identifier, it sets that current active database is associated with the specified link identifier . if you not set that link identifier it will associate with last link opened by mysql_connect().if it didn’t find any connection it return error message

Complete Example :: MySQL database connection with PHP

$host_name='localhost'; 
// mysql server host name here you can also use port number[example hostname:portnumber] and socket path [example ":path/to/socket"]
//$host_name='hostname:1085'; 
// port number
//$host_name=""; 
// socket path
$db_user='root';  
// mysql user name
$db_password=''; 
// mysql password
$database_name='test'; 
// mysql database name
 
$conn=mysql_connect($host_name,$db_user,$db_password) or die(mysql_error()); 
// connecting to mysql server
mysql_select_db($database_name,$conn) or die(mysql_error()); 
// selecting mysql database
// here $conn is an link identifier, it sets that current active database is associated with the specified  link identifier . if you not set that link identifier it will associate with last link opened by mysql_connect() 
mysql_close($conn); 
// $conn is link identifier
// mysql_connect(), open a connection to mysql server
//die(), exit the current running script and print error message
//mysql_error(), it return the error message from last mysql operation
//mysql_select_db(), it select the mysql database

Complete Example :: MySQL database connection with PHP in OPPs

opneConnection();
	}
	
	public function opneConnection() {
		$this->connection = mysql_connect('localhost','root','');
		if(!$this->connection) {
			die('Database Connection Failed:'. mysql_error());
		} else {
			$selected  = mysql_select_db('test',$this->connection);
			if(!$selected) {
				die('Database seletion Failed:'. mysql_error());
			}
		}
		
	}
	
	public function closeConnection() {
		if(isset($this->connection)) {
			mysql_close($this->connection);
			unset($this->connection);
		}
	}
	
}

// how to use
$database = new MysqlDatabase();

?>

Leave a Reply