Skip to content
Advertisement

Can I create a database using PDO in PHP?

I want to create a class which uses PDO to interact with MySQL. Can I create a new MySQL table using PDO?

Advertisement

Answer

Yes, you can.

The dsn part, which is the first parameter of the PDO constructor, does not have to have a database name. You can simply use mysql:host=localhost. Then, given you have the right privilege, you can use regular SQL commands to create a database and users, etc.

Following is an example from an install.php file. It logs in with root, create a database, a user, and grant the user all privilege to the new created database:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement