php
Mysql connectivity
Use the mysql_connect( ) function to established connection to the MySQL server. To access the database functionality we have to make a connection to database using PHP.  mysql_connect() function is used to establish the connection to mysql server.
Syntax

mysql_connect(hostname, username, password, dbname);

Parameter Description
hostname Either a hostname(server name) or an IP address
username The MySQL user name
password The password to log in with
dbname Optional. The database to be used when performing queries
MySQL Connection
In the following example we store the connection in a variable ($con) for later use in the script
<?php
 
 // Create connection
 $con=mysql_connect("localhost","root","") or die(mysql_error());
 
?>
Here localhost is server name. root is MySQL default user name. default password is blank and database name is php_mysql_classes. mysqli_error( ) function provides mysql connectivity error message.
MySQL Close Connection
<?php

// Create connection
$con=mysql_connect("localhost","root","","my_db") or die(mysql_error());	
//code to be executed...
 
// Close connection	
mysql_close($con);
   
?>
After work with the database is done we have to close the connection using mysql_close() function in which the connection to the database is passed.
MySQL Advanced Connection
  • MySQLi extension (the "i" stands for improved)
  • PDO (PHP Data Objects)

Earlier versions of PHP used the MySQL extension. While the PDO extension is more portable and supports more than twelve different databases, MySQLi extension as the name suggests supports MySQL database only. MySQLi extension however provides an easier way to connect to, and execute queries on, a MySQL database server.

MySQLi and PDO Syntax

In this, and in the following chapters we demonstrate three ways of working with PHP and MySQL :

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO

MySQLi Installation

For installation details, go to: http://php.net/manual/en/mysqli.installation.php

PDO Installation

For installation details, go to: http://php.net/manual/en/pdo.installation.php


Connecting to MySQL Database Server

You can use the mysqli_connect() function. All communication between PHP and the MySQL database server takes place through this connection.

Syntax: MySQLi - Procedural

$link = mysqli_connect("hostname", "username", "password", "database");

Syntax: MySQLi - Object Oriented

$mysqli = new mysqli("hostname", "username", "password", "database");

Syntax: MySQL - PDO(PHP Data Objects)

$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");

Examples :
The following example shows how to connect to MySQL database server using MySQLi (both procedural and object oriented way) and PDO extension.

MySQLi - Procedural
<?php

// variable is yor wish $conn or $ftl or $ftl_connect etc,.

// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "");

// Check connection
if (!$ftl_connect) {
    die("ERROR : Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

?>
MySQLi - Object-Oriented
<?php

// Create connection
$ftl_connect = new mysqli("localhost", "root", "");

// Check connection
if ($ftl_connect->connect_error) {
    die("ERROR : Connection failed: " . $ftl_connect->connect_error);
}
echo "Connected successfully";

?>
MySQL - PDO(PHP Data Objects)
<?php

try {
    $ftl_connect = new PDO("mysql:host=localhost;dbname=ftl_db", "root", "");
    // set the PDO error mode to exception
    $ftl_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
	die("ERROR : Connection failed: " . $e->getMessage());
    }
	
?>
MySQLi Close Connection
The connection will be closed automatically when the script ends. To close the connection before, use the following :
MySQLi - Procedural
<?php

// Create connection
$ftl_connect = mysqli_connect("localhost", "root", "");

// Check connection
if (!$ftl_connect) {
    die("ERROR : Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

//Close Connection
mysqli_close($ftl_connect);

?>
MySQLi - Object-Oriented
<?php

// Create connection
$ftl_connect = new mysqli("localhost", "root", "");

// Check connection
if ($ftl_connect->connect_error) {
    die("ERROR : Connection failed: " . $ftl_connect->connect_error);
}
echo "Connected successfully";

//Close Connection
$ftl_connect->close();

?>
MySQL - PDO(PHP Data Objects)
<?php

try {
    $ftl_connect = new PDO("mysql:host=localhost;dbname=ftl_db", "root", "");
    // set the PDO error mode to exception
    $ftl_connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
	die("ERROR : Connection failed: " . $e->getMessage());
    }
	
   // Close connection
   $ftl_connect = null;
  //or
  //unset($ftl_connect);
?>