Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pdo connection without database name?

Tags:

php

mysql

pdo

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?

like image 224
Yehonatan Avatar asked Jul 11 '12 12:07

Yehonatan


People also ask

Which type of databases can PDO connect to?

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!

How can you manually close a PDO connection?

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.


1 Answers

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');
like image 95
Ronn0 Avatar answered Oct 30 '22 10:10

Ronn0