Create database through PDO

0

I have a project in mind, in which I would like to create a database with the name of variables, and in case it would be checked to see if the DB was already created, it would look something like this:

    if ($pdo=new PDO ("mysql:host=localhost;dbname=$var","root",""))=sucess 
    {
       "não cria BD"
    }else{
       CREATE DATABASE $var
    }

Is this possible?

OBS : I do not really need to do this but it would be much more organized if I could.

    
asked by anonymous 09.10.2017 / 17:08

1 answer

0
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
    
10.10.2017 / 07:20