Create bank and table with PHP PDO Mysql

0

Good afternoon everyone, I'm a programmer and I'm having a problem, I'm creating a web store registration system, I created an administrative page to register the stores, in that I registered the store data with a responsible name and user and password to access because each store will have an access to your area, now the problem is that when I create the store I already create a database for it, even this is ok but I want to create the product table when creating the database and I can not, I wonder if it is possible to create the table soon after creating the database? I'm using PHP PDO and Mysql.

Thank you in advance.

    
asked by anonymous 17.04.2018 / 19:08

2 answers

0
  

Your answer does not answer your own question !!!

Yes you can create the table right after you create the database

try {
    $conn = new PDO("mysql:host=$servername", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "CREATE DATABASE IF NOT EXISTS lojaDB";
    $conn->exec($sql);
    $sql = "use musicDB";
    $conn->exec($sql);
    $sql = "CREATE TABLE IF NOT EXISTS PRODUTOS (
                ID int(11) AUTO_INCREMENT PRIMARY KEY,
                produto varchar(30) NOT NULL)";
    $conn->exec($sql);
    echo "DB criado com sucesso";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
    
18.04.2018 / 04:54
0

I was able to solve it, the problem was that when I created the bank there was no bank selected in the connection, so I made a query selecting the name of the bank that was created and there it creates the table inside the new bank. Thanks people

    
17.04.2018 / 19:34