Create Table Using PHP Data Object(PDO)
![]() |
PHP Database Table Creation using PDO |
Hi Guys In This Tutorial we will Learn how to Create a Table using Php Data Object(PDO).
using the following example we create a table named "test" under "tutorial" database.
create a new file named table.php in htdocs folder and paste the below codes.
Codes for table.php
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 26 27 28 29 30 31 32 33 | <?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 $db_name = "tutorial" ; //Database Name try { //Creating Connection between Server and Database $conn = new PDO( "mysql:host=$host;dbname=$db_name" , $username , $password ); // SET PDO error mode to exception $conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Creating New table using SQL Query $sql = "CREATE TABLE test ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(150) NOT NULL, email varchar(150) NOT NULL, PRIMARY KEY (id) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;"; //exec() Function because no results are returned $conn -> exec ( $sql ); echo "You have Successfully Created test Table" ; } 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.