create table using pdo in php
<?php//create table using pdo
$server = "localhost";$username = "root";$password = ""; // blank$dbname = "demo1"; // what if we type wrong?
try { $conn = new pdo("mysql:host=$server;dbname=$dbname",$username,$password); //no need to add primary key and auto increment manually // it will give automatic if you write this query $sql="CREATE TABLE IF NOT EXISTS `register` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(30) NOT NULL, `password` varchar(15) NOT NULL, `mobile_no` varchar(10) NOT NULL, `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1"; $conn->exec($sql); echo "create table successfully";} catch (\Throwable $th) { echo $sql . "<br>". $th->getMessage();}$conn = null;?>
if you have still doubt please see out video:
<?php
//create table using pdo
$server = "localhost";
$username = "root";
$password = ""; // blank
$dbname = "demo1"; // what if we type wrong?
try {
$conn = new pdo("mysql:host=$server;dbname=$dbname",$username,$password);
//no need to add primary key and auto increment manually
// it will give automatic if you write this query
$sql="CREATE TABLE IF NOT EXISTS `register` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(15) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$conn->exec($sql);
echo "create table successfully";
} catch (\Throwable $th) {
echo $sql . "<br>". $th->getMessage();
}
$conn = null;
No comments:
Post a Comment
If You have any doubt, or want to know about something, please let me know