Tech Update

The Best Tech Research

PHP PDO MySQL – Easy Illustration Connecting to MySQL With PDO Class

PHP PDO MySQL – Easy Illustration Connecting to MySQL With PDO Class

I am going to show a basic illustration on how to link to MySQL making use of PHP’s PDO course. Just some of the rewards of PDO is that it really is fast and if you use the PDO::put together() method it will reduce SQL injection assaults by calling the PDO::quotation() process. The other pros is that there are quite a few databases it will assist. So let us dive suitable into the code:

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


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

echo ‘Connected to database’

capture(PDOException $e)

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

Just an critical observe that if you obtain the subsequent sort of lethal mistake in your improvement ecosystem:

Deadly mistake: Uncaught exception ‘PDOException’ with information ‘SQLSTATE[42000] [1049] Unfamiliar database ”user”’ in C:Plan FilesApache Software program FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.class.inc:30 Stack trace: # C:Program FilesApache Software program FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.course.inc(30): PDO->__assemble(‘mysql:host=loca…’, ‘username’, ‘password’) #1 C:System FilesApache Software package FoundationApache2.2htdocstesttrunkcodelogin1classesstd.mysql.course_test.inc(43): db::getConnect() #2 C:System FilesApache Application FoundationApache2.2htdocstesttrunkcodelogin1connect.php(6): MySqlDb->confirmUserPass(‘usertest’, ‘passtest’) #3 main thrown in C:Application FilesApache Software program FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.course.inc on line 30

Seems to be quite messy and challenging to decipher. When striving to decipher error code I normally seem at the to start with mistake which led me to see why it was reporting an “Unfamiliar database” when it was present. The extra offers also gave me a trace as to the difficulty. So I concluded the challenge resulted in the placement of additional prices around the values of host and/or dbname. The next will produce the former mistake:

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

So if you do not use variables then do not insert the single estimates for the values of host and dbname. In other words, use the following code instead:

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

This is a uncomplicated take a look at to hook up to your mysql databases or any other assistance database. Just for your information and facts if you want to connect to PostgreSQL which is one more well-known and strong databases use the subsequent code in put of the line of instantiation:

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

If you’re in a development atmosphere and want to screen your mistakes instantly to the monitor you can specify the mistakes shown. You need to have to established your exhibit_mistakes configurations to ‘on’ in your php.ini file. So set your mistake attributes just after instantiating the PDO class like so:


There are a few kinds of mistake report settings for PDO::ATTR_ERRMODE:
PDO::ERRMODE_SILENT = mistake codes
PDO::ERRMODE_EXCEPTION = Toss exceptions

Here is an instance of applying PDO::ATR_ERRMODE:

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


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

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


$sql = ‘Select * from tutorialref where by id=1’

$final result = $db->query($sql)

foreach ($outcome as $row)

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

$db = null // close the database link

catch(PDOException $e)

echo $e->getMessage()

Don’t confuse this with the errors generated from the php setting of mistake_reporting. The problems from PDO::ATTR_ERRMODE implement to the sql question and its outcomes. I will dive into the mistake configurations and the distinctive outputs of the php.ini error_reporting settings and PDO::ATTR_ERRMODE report options in a long term posting.