While mysql_connect has been depreciate, there is only two way to connect database in php
1. mysli_
2. PDO (the best and safest way).
1. mysli_
2. PDO (the best and safest way).
below is the code
<?php
define ('DB_HOST', "127.0.0.1");
define ('DB_USER', "root");
define ('DB_PASSWORD', "");
define ('DB_DATABASE', "database_name");
try
{
$pdo = new PDO('mysql:host='. DB_HOST .';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'its connect';
}
catch (PDOException $e)
{
$pdo='Error: '.$e->getMessage();
die('DB execution failed due to this error <br />'.$e->getMessage());
}
//print_r($pdo);
return $pdo;
?>
<?php
define ('DB_HOST', "127.0.0.1");
define ('DB_USER', "root");
define ('DB_PASSWORD', "");
define ('DB_DATABASE', "database_name");
try
{
$pdo = new PDO('mysql:host='. DB_HOST .';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'its connect';
}
catch (PDOException $e)
{
$pdo='Error: '.$e->getMessage();
die('DB execution failed due to this error <br />'.$e->getMessage());
}
//print_r($pdo);
return $pdo;
?>
No comments:
Post a Comment