Mysqli
<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=NewMysqli ($servername,$username,$password); //Detecting Connections
if($conn-connect_error) {
die("Connection failed:".)$conn-connect_error); }
Echo"Connection succeeded";
?>
Pdo
<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; Try {
$conn=NewPDO ("mysql:host=$servername;d Bname=mydb ",$username,$password); Echo"Connection succeeded"; }
Catch(pdoexception$e)
{
Echo $e-getMessage ();} ?>
Close link
Example (Mysqli- object oriented)
$conn->close ();
Example (MYSQLI- process-oriented
)mysqli_close($conn);
instance (PDO)
$conn null;
Create a database
Example (Mysqli-Object-oriented)<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=NewMysqli ($servername,$username,$password); //Detecting Connections
if($conn-connect_error) {
die("Connection failed:".)$conn-connect_error); }
//Create a database
$sql= "CREATE DATABASE MyDB";
if($conn->query ($sql) ===TRUE) {
Echo"Database Creation succeeded";
} Else {
Echo"Error Creating Database:".$conn-error;} $conn-close ();?>
Example (mysqli procedural)<?PHP$servername= "localhost";
$username= "username";
$password= "Password"; //Create a connection
$conn=Mysqli_connect($servername,$username,$password); //Detecting Connections
if(!$conn) {
die("Connection failed:".)Mysqli_connect_error()); }
//Create a database
$sql= "CREATE DATABASE MyDB";
if(Mysqli_query($conn,$sql)) {
Echo"Database Creation succeeded";
} Else {
Echo"Error Creating Database:".Mysqli_error($conn); }
Mysqli_close($conn); ?>
Using PDO:<?PHP$servername= "localhost"; $username= "username"; $password= "Password"; Try {
$conn=NewPDO ("mysql:host=$servername;d Bname=mydb ",$username,$password); //Set PDO error mode to exception
$conn->setattribute (Pdo::attr_errmode, PDO::errmode_exception); $sql= "CREATE DATABASE mydbpdo"; //use EXEC (), because no results are returned
$conn-exec($sql); Echo"Database Creation Success <br>"; }
Catch(pdoexception$e)
{
Echo $sql. "<br>".$e-GetMessage (); }
$conn=NULL; ?>
Mysqli & PDO