PHP PDO MySQL – Simple Example Connecting to MySQL With PDO Class

I am going to exhibit a very simple case in point on how to link to MySQL making use of PHP’s PDO class. Just some of the added benefits of PDO is that it’s fast and if you use the PDO::prepare() method it will prevent SQL injection assaults by calling the PDO::quotation() approach. The other pros is that there are several databases it will support. So let’s dive appropriate into the code:

$hostname = ‘localhost’
$username = ‘your_username’
$password = ‘your_password’


$db = new PDO(“mysql:host=$hostnamedbname=mysql”, $username, $password)

echo ‘Connected to database’

catch(PDOException $e)

echo $e->getMessage()
Lethal Mistake new PDO Instance

Just an important note that if you acquire the adhering to form of fatal mistake in your progress natural environment:

Lethal mistake: Uncaught exception ‘PDOException’ with concept ‘SQLSTATE[42000] [1049] Mysterious database ”user”’ in C:System FilesApache Software package Stack trace: # C:Program FilesApache Software package PDO->__construct(‘mysql:host=loca…’, ‘username’, ‘password’) #1 C:Software FilesApache Software db::getConnect() #2 C:System FilesApache Software program FoundationApache2.2htdocstesttrunkcodelogin1connect.php(6): MySqlDb->confirmUserPass(‘usertest’, ‘passtest’) #3 principal thrown in C:Plan FilesApache Application on line 30

Looks pretty messy and challenging to decipher. When seeking to decipher mistake code I ordinarily look at the first error which led me to see why it was reporting an “Unknown databases” when it was current. The additional rates also gave me a trace as to the challenge. So I concluded the challenge resulted in the placement of additional offers all around the values of host and/or dbname. The pursuing will create the previous error:

$db = new PDO(“mysql:host=’localhost’dbname=’mysql'”, $username, $password)

So if you do not use variables then do not increase the one quotes for the values of host and dbname. In other words and phrases, use the pursuing code in its place:

$db = new PDO(“mysql:host=localhostdbname=mysql”, $username, $password)

This is a straightforward take a look at to join to your mysql database or any other aid database. Just for your info if you want to hook up to PostgreSQL which is one more common and strong databases use the next code in position of the line of instantiation:

$db = new PDO(“pgsql:dbname=pdohost=localhost”, “username”, “password” )

If you might be in a improvement ecosystem and wish to show your faults straight to the display you can specify the problems exhibited. You need to have to established your display_glitches settings to ‘on’ in your php.ini file. So set your mistake attributes immediately after instantiating the PDO course like so:


There are a few varieties of error report configurations for PDO::ATTR_ERRMODE:
PDO::ERRMODE_SILENT = mistake codes
PDO::ERRMODE_EXCEPTION = Throw exceptions

Here’s an instance of implementing PDO::ATR_ERRMODE:

$hostname = ‘localhost’
$username = ‘your_username’
$password = ‘your_password’


$db = new PDO(“mysql:host=$hostnamedbname=posts”, $username, $password)

echo ‘Connected to database’ // check out for connection


$sql = ‘Select * from tutorialref the place id=1’

$outcome = $db->query($sql)

foreach ($end result as $row)

echo $row[‘id’] .’ – ‘. $row[‘author’] . ”

$db = null // shut the databases relationship

catch(PDOException $e)

echo $e->getMessage()

Do not confuse this with the errors created from the php environment of mistake_reporting. The faults from PDO::ATTR_ERRMODE use to the sql query and its effects. I will dive into the mistake configurations and the different outputs of the php.ini error_reporting configurations and PDO::ATTR_ERRMODE report options in a potential posting.

Leave a Reply

Your email address will not be published.