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 |
<?php
// Create connection
$con=mysql_connect("localhost","root","") or die(mysql_error());
?>
mysqli_error( )
function provides mysql connectivity error message.<?php
// Create connection
$con=mysql_connect("localhost","root","","my_db") or die(mysql_error());
//code to be executed...
// Close connection
mysql_close($con);
?>
mysql_close()
function in which the connection to the database is passed.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.
In this, and in the following chapters we demonstrate three ways of working with PHP and MySQL :
For installation details, go to: http://php.net/manual/en/mysqli.installation.php
For installation details, go to: http://php.net/manual/en/pdo.installation.php
You can use the mysqli_connect()
function. All communication between PHP and the MySQL database server takes place through this connection.
$link = mysqli_connect("hostname", "username", "password", "database");
$mysqli = new mysqli("hostname", "username", "password", "database");
$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");
<?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";
?>
<?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";
?>
<?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());
}
?>
<?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);
?>
<?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();
?>
<?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);
?>