How to create a MySQL Table Using PDO in PHP

1 Answer

0 votes
$db_host        = 'localhost';
$db_user        = 'root';
$db_password    = '';
$db_name        = 'allonpage';

try 
{
    $con = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "CREATE TABLE Worker
            (
                WorkerID int,
                LastName varchar(255),
                FirstName varchar(255)
            );";

    $con->exec($sql);
    echo "Table created successfully";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}

$con = null;

 

/*
run: 

Table created successfully

*/

 



answered Jul 6, 2016 by avibootz

Related questions

...