How to Create MySQL Database using PHP Data Objects(PDO)
![]() |
| Create MySQL Database using PDO |
Hi Guys in This tutorial we will Learn how to Create a Database in MySQL using PHP Data Objects(PDO).
The following PDO example create a database named "tutorial"
<?php
$host = "localhost"; //Host Name
$username = "root"; //MySQL Username.if you don't know leave 'root' by Defaults
$password = ""; //MySQL Password if Password Protected .else Leave it Blank
try {
//Connecting To The Server
$conn = new PDO("mysql:host=$host", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//Creating New Database ||Database name: tutorial
$sql = "CREATE DATABASE tutorial";
// Use exec() Cause no Result returned
$conn->exec($sql);
echo "Your Database successfully Created<br>";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
If you Like This article Please Like and Share With Your Friends.
Keep Visiting for More Codes.Thank you.


No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.