Mysql database connection
$database_host = 'localhost'; // database host name
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = mysql_connect($database_host, $database_user, $database_pass);
// check database connection
if(! $database_conn )
{
// error in database connection
die('Could not connect to database : ' . mysql_error());
}
// connected to database
echo 'Connected successfully with database';
// close connection with Mysql database
mysql_close($database_conn);
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = mysql_connect($database_host, $database_user, $database_pass);
// check database connection
if(! $database_conn )
{
// error in database connection
die('Could not connect to database : ' . mysql_error());
}
// connected to database
echo 'Connected successfully with database';
// close connection with Mysql database
mysql_close($database_conn);
MySQLi Object-Oriented database connection
$database_host = 'localhost'; // database host name
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = new mysqli($database_host, $database_user, $database_pass);
// check database connection
if ($database_conn->connect_error)
{
// error in database connection
die("Could not connect to database : " . $database_conn->connect_error);
}
// connected to database
echo 'Connected successfully with database';
// close connection with MySQLi database
$database_conn->close();
$database_host = 'localhost'; // database host name
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = new mysqli($database_host, $database_user, $database_pass);
// check database connection
if ($database_conn->connect_error)
{
// error in database connection
die("Could not connect to database : " . $database_conn->connect_error);
}
// connected to database
echo 'Connected successfully with database';
// close connection with MySQLi database
$database_conn->close();
MySQLi Procedural database connection
$database_host = 'localhost'; // database host name
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = mysqli_connect($database_host, $database_user, $database_pass);
// check database connection
if (!$database_conn)
{
// error in database connection
die("Could not connect to database : " . mysqli_connect_error());
}
// connected to database
echo 'Connected successfully with database';
// close connection with MySQLi Procedural
mysqli_close($database_conn);
$database_host = 'localhost'; // database host name
// if using port then add port $database_host = 'localhost:3036';
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
// connect with database
$database_conn = mysqli_connect($database_host, $database_user, $database_pass);
// check database connection
if (!$database_conn)
{
// error in database connection
die("Could not connect to database : " . mysqli_connect_error());
}
// connected to database
echo 'Connected successfully with database';
// close connection with MySQLi Procedural
mysqli_close($database_conn);
PDO database connection
$database_host = 'localhost'; // database host name
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
$database_name = 'your_database_name'; // database name
try
{
$database_conn = new PDO("mysql:host=$database_host;dbname=$database_name", $database_user, $database_pass);
// if using port then use port id in PDO
//$database_conn = new PDO('mysql:host=$database_host;port=5432;dbname=$database_name', $database_user, $database_pass);
// exception for PDO connection error
$database_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// connected to database
echo 'Connected successfully with database';
}
catch(PDOException $exception)
{
// error in database connection
echo "Could not connect to database : " . $exception->getMessage(); // exception
}
// close connection with PDO
$database_conn = null;
$database_host = 'localhost'; // database host name
$database_user = 'database_user'; // database user name
$database_pass = 'database_password'; // database user password
$database_name = 'your_database_name'; // database name
try
{
$database_conn = new PDO("mysql:host=$database_host;dbname=$database_name", $database_user, $database_pass);
// if using port then use port id in PDO
//$database_conn = new PDO('mysql:host=$database_host;port=5432;dbname=$database_name', $database_user, $database_pass);
// exception for PDO connection error
$database_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// connected to database
echo 'Connected successfully with database';
}
catch(PDOException $exception)
{
// error in database connection
echo "Could not connect to database : " . $exception->getMessage(); // exception
}
// close connection with PDO
$database_conn = null;
0 comments: