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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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 "; } catch (PDOException $e ) { echo $sql . " " . $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.