I don't understand what is wrong with the code below. Everything seems fine but I'm still getting a notice:
Notice: Undefined variable: sql in C:\xampp\htdocs\w3s\createtablepdo.php on line 27 " and error could not find driver.
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="mysql";
try
{
//creating connection
$conn = new PDO("mysqli:host=$servername;dbname=$dbname",$username,$password);
//checking exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
//use exec cos no result are returned
$conn->exec($sql);
echo "table MyGuests created succesfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>