PHP PDO MySQL – Very simple Illustration Connecting to MySQL With PDO Course

Posted in WordPress on Jul 15, 2014

I’ll display a uncomplicated case in point on how to join to MySQL using PHP’s PDO class. Just some of the positive aspects of PDO is that it can be speedy and if you use the PDO::prepare() method it will avert SQL injection attacks by contacting the PDO::quotation() strategy. The other pros is that there are several databases it will assist. So let’s dive correct into the code:

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

test

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

echo ‘Connected to database’

catch(PDOException $e)

echo $e-&gtgetMessage()
Deadly Error new PDO Instance

Just an important be aware that if you acquire the next sort of lethal mistake in your improvement surroundings:

Lethal error: Uncaught exception ‘PDOException’ with message ‘SQLSTATE[42000] [1049] Mysterious database ”user”’ in C:Program FilesApache Software FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.course.inc:thirty Stack trace: # C:Program FilesApache Software FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.class.inc(thirty): PDO-&gt__build(‘mysql:host=loca…’, ‘username’, ‘password’) #1 C:Program FilesApache Software package FoundationApache2.2htdocstesttrunkcodelogin1classesstd.mysql.class_exam.inc(43): db::getConnect() #two C:Program FilesApache Computer software FoundationApache2.2htdocstesttrunkcodelogin1connect.php(6): MySqlDb-&gtconfirmUserPass(‘usertest’, ‘passtest’) #3 key thrown in C:Program FilesApache Application FoundationApache2.2htdocstesttrunkcodelogin1classesstd.pdo_singleton.course.inc on line thirty

Seems really messy and tough to decipher. When trying to decipher error code I ordinarily glance at the 1st mistake which led me to see why it was reporting an “Unfamiliar databases” when it was present. The extra offers also gave me a hint as to the difficulty. So I concluded the dilemma resulted in the placement of more offers close to the values of host and/or dbname. The next will crank out the earlier mistake:

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

So if you do not use variables then do not include the one offers for the values of host and dbname. In other words, use the subsequent code alternatively:

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

This is a uncomplicated take a look at to hook up to your mysql database or any other guidance databases. Just for your information if you desire to link to PostgreSQL which is a further popular and robust databases use the following code in place of the line of instantiation:

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

If you might be in a growth natural environment and desire to screen your errors immediately to the display you can specify the problems displayed. You want to set your display_problems configurations to ‘on’ in your php.ini file. So established your error attributes following instantiating the PDO class like so:

$db-&gtsetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)

There are three styles of mistake report configurations for PDO::ATTR_ERRMODE:
PDO::ERRMODE_SILENT = error codes
PDO::ERRMODE_WARNING = E_WARNING
PDO::ERRMODE_EXCEPTION = Throw exceptions

This is an instance of employing PDO::ATR_ERRMODE:

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

try

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

echo ‘Connected to database’ // verify for link

$db-&gtsetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING)

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

$final result = $db-&gtquery($sql)

foreach ($result as $row)

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

$db = null // close the database connection

capture(PDOException $e)

echo $e-&gtgetMessage()

Never confuse this with the glitches created from the php placing of error_reporting. The glitches from PDO::ATTR_ERRMODE use to the sql question and its results. I will dive into the error settings and the distinct outputs of the php.ini mistake_reporting settings and PDO::ATTR_ERRMODE report configurations in a future article.

By Victor Kimura

Leave a Reply

Your email address will not be published. Required fields are marked *