I'm trying to create a CMS and I want to make my client life easier so I create the database by myself.
I encountered a problem while trying to do this. The database isn't created so I can't connect with a PDO connection and mysql
is deprecated so I can't use it. Do you have any advice for me on how I can create a PDO connection before the database is created? like a mysql_select_db()
alternative?
PDO stands for PHP Data Object. Unlike MySQLi, PDO is only object-oriented and supports a number of different database types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL. Important!
The connection remains active for the lifetime of that PDO object. To close the connection, you need to destroy the object by ensuring that all remaining references to it are deleted—you do this by assigning null to the variable that holds the object.
You can start a PDO connection by this, correct me if I'm wrong:
$db = new PDO("mysql:host=localhost", 'user', 'pass');
The difference from this with a normal connection is the removed part dbname=[xx]
, as you see.
Also a free tip when you want to use a UTF-8 connection:
$db = new PDO("mysql:host=localhost;charset=utf8mb4", 'user', 'pass');
In order to the comment of Yehonatan
you can select a database by:
$db->exec('USE databaseName');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With