PHP PDO MySQL – Simple Example Connecting to MySQL With PDO Class
I’ll demonstrate a simple example on how to connect to MySQL using PHP’s PDO class. Just some of the benefits of PDO is that it’s fast and if you use the PDO::prepare() method it will prevent SQL injection attacks by calling the PDO::quote() method. The other pros is that there are several databases it will support. So let’s dive right into the code:
Fatal Error new PDO Instance
Just an important note that if you receive the following type of fatal error in your development environment:
Looks pretty messy and hard to decipher. When trying to decipher error code I usually look at the first error which led me to see why it was reporting an “Unknown database” when it was existing. The extra quotes also gave me a hint as to the problem. So I concluded the problem resulted in the placement of extra quotes around the values of host and/or dbname. The following will generate the previous error:
So if you do not use variables then do not add the single quotes for the values of host and dbname. In other words, use the following code instead:
This is a simple test to connect to your mysql database or any other support database. Just for your information if you wish to connect to PostgreSQL which is another popular and robust database use the following code in place of the line of instantiation:
If you’re in a development environment and wish to display your errors directly to the screen you can specify the errors displayed. You need to set your display_errors settings to ‘on’ in your php.ini file. So set your error attributes after instantiating the PDO class like so:
There are three types of error report settings for PDO::ATTR_ERRMODE:
PDO::ERRMODE_SILENT = error codes
PDO::ERRMODE_WARNING = E_WARNING
PDO::ERRMODE_EXCEPTION = Throw exceptions
Here’s an example of implementing PDO::ATR_ERRMODE:
Don’t confuse this with the errors generated from the php setting of error_reporting. The errors from PDO::ATTR_ERRMODE apply to the sql query and its results. I’ll dive into the error settings and the different outputs of the php.ini error_reporting settings and PDO::ATTR_ERRMODE report settings in a future article.
|
PHP Tutorial, tips, guides. Learn PHP programming. Victor Kimura |
Tags: php
Other News
- PHP: Hypertext Preprocessor
- Discover the Single Marquee Tools/ Pixel Stretch Technique
- Define Your Own WordPress Loop Using WP_Query
- PHP PDO MySQL – Simple Example Connecting to MySQL With PDO Class
- Mastering CSS with DW CS3
- Ajax: How To Weave A Faster Web
- Say goodbye to SEO as you know it, latent semantic indexing explained
- Discover the Magic Wand for Image Adjusting-part 2
- My Widget – example WordPress widget
- How to SEO your site: Episode 4 – Social news as an SEO tool
-

1.CSS+DIV+HTML
Do a framework for the website
Read More | Postd by Ltm.ming -

2.PhotoShop CS
To Beautify the site
Read More | Postd by Ltm.ming -

3.JavaSprict-Script
For User experience
Read More | Postd by Ltm.ming -

4.Programming language
php is very powerful
Read More | Postd by Ltm.ming -

5.SQL & MYSQL
For Information Storage
Read More | Postd by Ltm.ming

PHP and MySQL 5
Beginning PHP 5 and MySQL: From Novice to Professional offers a comprehensive introduction to two of the most popular open source technologies on the planet: the PHP scripting language and the MySQL database server.
Read More | Postd by Ltm.ming
Base AJAX Codes and Examples
Make your Web pages stand out above the noise with JavaScript and the expert instruction in this much-anticipated update to the bestselling JavaScript Bible.
Read More | Postd by Ltm.ming
Mastering CSS with DW CS3
Durch Integration von Datenbankanbindung und Skriptsprachen wird Dreamweaver CS 4 zum alltagstauglichen Werkzeug für die Erstellung einfacher Redaktionssysteme.
Read More | Postd by Ltm.ming
Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites.
Read More | Postd by Ltm.ming
MySQL 5.0 Certification Study Guide
This book is a very comprehensive guide to what MySQL offers and can teach you. br / br /Would definitely recommend for anyone needing a great book about MySQL.
Read More | Postd by Ltm.ming