Create database through PDO

Asked

Viewed 148 times

0

I have a project in mind, in which I would like to create database with the name of variables, and in case it would be done a check to know if the BD has been created, would be something like this:

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

That’s possible?

OBS: I don’t really need to do this but it would be much more organized if possible.

  • 2

    Every time you do something you don’t need you get less organized.

  • In the case of the code yes, but the BD would be even more so if this is not possible.

  • 1

    I don’t see how this makes sense, but you can try to show how.

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();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.