Create a MySQL Database
Creating a database is a little different from the connection exercise you just did. Before, PHP connected to a database that already existed. Here, the goal is to connect to the MySQL server first and then ask it to create a brand new database.
That is why the MySQLi example only uses host, username, and password. There is no database name yet, because the database you want does not exist at that point. First you connect to the server, then you run the SQL command that creates the database.
The actual SQL part is very small: CREATE DATABASE some_name. What matters more is understanding who is doing what. PHP opens the connection, MySQL receives the SQL command, and then MySQL either creates the database or returns an error.
The other important detail in this lesson is permissions. Creating or deleting databases is not normal day-to-day application behavior. It usually requires stronger privileges. So if this works in your local environment, good. If it fails with a privilege error, that is not you “doing PHP wrong” – it just means that MySQL refused the request because that user is not allowed to create databases.
You also saw the same split as before: MySQLi can check success manually, while PDO often leans on exceptions. Same idea underneath, though. Try the action, and handle failure cleanly instead of assuming it worked.
So the useful takeaway here is: when creating a new database, connect to the server without selecting a database first, run CREATE DATABASE ..., and be aware that success depends not only on your PHP syntax but also on the permissions of the MySQL user.