Create database and table with PHP PDO Mysql

Asked

Viewed 1,350 times

0

Good afternoon to all, I am inciante in programming and I am with a problem, I am creating a web registration system of stores, I created an administrative page to register the stores, in this I register the store data with name responsible type and user and password for 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, until this is ok but I want to create the product table when creating the bank and I can’t, I wonder if it is possible to create the table right after creating the bank? I am using PHP PDO and Mysql.

Thanks in advance.

  • It may be that the bank is not yet ready, you may not be able to create one right after the other.

  • the bank creates, but the table does not.

  • Karlos Fist welcome!! An interesting post that will help you with your next questions. Example minimo https://answall.com/help/mcve How not to ask questions https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask questions and this is also interesting https://pt.meta.stackoverflowcom/questions/1078/como-e-por-que-acceptuma-questionsReply/1079#1079

2 answers

0


Your answer does not answer your own question!!!

Yes it is possible to create the table right after creating 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();
}

0

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

Browser other questions tagged

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