Article Side

Online Article Directory!

Hello Guest! Login or Register to submit articles!

Optimum PHP Database Connectivity - the PDO Way

By James Andy Subscribe to RSS | June 25th 2012 | Views:
loading
 
  



Hypertext Processor or PHP, as it is more commonly known, has entranced the minds of developers worldwide. It is a general-purpose scripting language that can be embedded in HTML. One of the major advantages of PHP is its ability to talk with a variety of databases. This adds the much-needed interactive dimension to websites whereusers can fetch dynamic data and perform transactions. The heart of any web application is not its UI or its controller; it’s the database that stores and provides data based on the queries that are made to it. PHP, being a widely popular programming language, is adopted across the world by dedicated PHP programmers handling large-scale projects for firms providing PHP Web Development Services. If you are one the countless offshore PHP developers who have handled database interactions, and still use the traditional PHP MySQL API for database connections, then this article is precisely targeted at you.

Did you know PHP offers 3 types of APIs for database connectivity? No? Well, here’s some information on them:

MySQL – The most popular API which establishes database connectivity in the easiest of manners.

MySQLi – An ‘Improved’ version of the MySQL API.

PDO – PHP Data Objects, an API that is database agnostic and supports prepared statements, thus negating the requirement for SQL Injection.

Looking at the above points, some might say feel that MySQL seems to be the easiest way out, then why should we try anything else! Well, let us give you three reasons. Firstly, some of the commonly used MySQL API database connectivity code snippets are all set to be deprecated as they do not adhere to the best practices policy are not exactly the right learning that greenhorn developers should pick up. Second, if you need to escape user input, you need to specifically do it from the code, which can be a complex endeavor, especially if you are new to database connectivity and don’t have a clue about data sanitization. Third and the most important point; the MySQL API is inflexible which means you will come up a cropper if you decide to switch databases. Reason enough to consider PDO?

Remember the traditional way of connecting to a MySQL database:

# Connect

mysql_connect('localhost', 'username', 'password') or die('Could not connect: '. mysql_error());

Now consider the PDO technique where we create a new class instance, specify the driver, database name, username, and password:

$conn= newPDO('mysql:host=localhost;dbname=myDatabase', $username, $password);

The main advantage of this technique is that in the event of switching of databases, you can simply update the DSN. This reduces your dependency of MySQL and eliminates the absolute necessity of functions, such as mysql_connect.

But what about errors, you will ask! If the database connection is not established, how do we check the error? PDO offers 3 error mode options to deal with exceptions:

PDO::ERRMODE_SILENT

PDO::ERRMODE_WARNING

PDO::ERRMODE_EXCEPTION

The default error mode is PDO::ERRMODE_SILENT, which does not throw up any exceptions. You must remember to change the default mode to PDO::ERRMODE_EXCEPTION which throws up exceptions as soon as they occur and stops the execution of the script. You can write a simple try-catch code block to capture these exceptions. Failure to do so will require you to manually fetch these non-captured errors.

Now comes the most important part, the core function; retrieving information from the database. This can be done using two options, query and execute. The query method involves using the PDO::Quote method to manually escape data and is almost similar to the mysql_real_escape_string. A word of advice here; if you are dealing with data that a user fills in to a form, use prepared statements instead. For any other data that is not form data, the query method is ideal.

Prepared statements are used via the prepare method that use named parameters, eliminating the need for SQL injection. These statements are then executed by passing data into the named placeholders. Now we come to the part of receiving the data that is fetched by prepared statements.

The PDO API provides multiple options to interact with the result. These include:

PDO::FETCH_ASSOC – default response, returns an array

PDO::FETCH_BOTH – returns an array, indexed by both column-name, and 0-indexed

PDO::FETCH_BOUND – returns TRUE and uses PHP variable for assignment of values in the columns

PDO::FETCH_CLASS – returns a new instance of the class used

PDO::FETCH_OBJ – returns an object that contains property names associated with columns

The PDO prepare extension is especially suited to the execution on the same query multiple times, each time with separate parameters. You can easily perform the common CRUD tasks that every PHP developer handles day in and day out. But the advantage that rises above all these features, is the usage of an object or class instance for mapping query results. These inherent advantages should be proof enough for you to switch over from your old MySQL API to PDO. It will not only make your code more secure, but also streamline it to a great extent. And who can forget the coveted flexibility to switch over to any database! All of you, the PHP developer community including all offshore PHP developers, go PDO now, if you haven’t already!

We are GoodCore, a leading Offshore Software Development Company in Malaysia. Having garnered expertise in every cutting-edge web development technology that emerges in the market, we apply our skills to the conceptualization, development and deployment of clutter-breaking solutions for clients across the world. We ensure that our team of developers, programmers and consultants are on top of every new development in the technology arena and are primed to lend that advantage to your product. Over the years, we have gained the trust and credibility of many elite clients across the globe. We wish to extend the same quality of services to you. You can hire dedicated PHP developers from us who will help develop cost-effective solutions that match your requirements to the T. If you wish to develop state-of-the-art PHP solutions that are not just path-breaking, but also ROI-intensive, don’t hesitate to approach us. Our vast repertoire of talent is at your service!

Source: http://software-development-technologies.blogspot.com/

James Andy - About Author:
James Andy is technical writer and specialized in software development technologies. He has past five years experience of technical content writing, his strong area is Microsoft .net development technologies.

Share on Facebook Tweet It Stumbleupon this post This post is delicious !

Article Source:
http://www.articleside.com/software-articles/optimum-php-database-connectivity-the-pdo-way.htm

Related Software Articles Subscribe to RSS

Digital Video Encoding services in affordable prices
Published by Seeman on August 19th 2012 | Software
Liquidbroadcast" is offering a deal for business users who need to boost their online video content....
 
Understand the Four Primary Significant Parts in the iPhone application development Methods?
Published by Jared Oates on May 14th 2012 | Computer
iPhone application development has actually obtained incredible worldwide popularity nowadays. It's ...
 
Testing is a necessary part in web development solutions that each and every developer should be able to plan well
Published by Williamsalas on June 11th 2012 | Software
Another crucial part in web development solutions is definitely testing. Testing ought to be carried...
 
New Trends in Mobile application development
Published by Etisbewcorp on August 23rd 2012 | Software
Mobile Application Development methodology
With the consumer market getting giddy in anticipatio...
 
Precisely what is Financial Automation Software development most relating to and just what does it embody?
Published by Josejames on June 12th 2012 | Software
As a software developer, you could present a variety of services to customers. Yet, you must bear in...
 
Flexible Offshore IT services available across the globe
Published by Terry Littrell on June 20th 2012 | Software
We are living in a world where each one is trying to out right the other. This is only increasing th...
 
Android mobile application development
Published by LizaFox on June 4th 2012 | Software
Unlike the iPhone which has exclusive manufacturer of the phones, Android has a number of handset ma...
 
Iphone application development – a dynamic power for your iphone
Published by Isol on January 9th 2012 | Software
The iPhone is a power product from the house of the world famous Apple Inc. Ever since Apple has joi...
 
Drupal development is the most powerful among the other CMS
Published by Sam Ward on July 13th 2012 | Software
In a decade if you carefully notice, there are so many open source CMS flooding in the market. Many ...
 
Po Web Design- Experience a different perspective of Swansea SEO services
Published by Powebdesign on July 6th 2012 | Computer
Having a well designed business or personal website out there hosted on the internet might not do mu...
 
E-Commerce Portals: Ask for services of a Magento Developer
Published by Terry Littrell on June 11th 2012 | Software
Continuing to set landmarks in contributing in developing e-commerce platforms, Magento Developers a...
 
India is one of the most desirable offshore software development countries!
Published by Sumitdass on August 23rd 2012 | Software
Software development is the need of every organization. Standard ‘off-the-shelf’ software or a c...
 
Do financial institutions derive good mileage out of mobile banking services
Published by Angel on April 3rd 2012 | Software
Financial institutions are offering mobile banking services to afford convenience banking to custome...
 
A2ZPC offers best affordable PC and laptop repair services
Published by Meghnath Kumar on May 19th 2012 | Computer
Everyone is well aware of what computers mean to us. Computers have made our professional and daily ...
 
Custom software services are best offered in Scottsdale
Published by Liza Andy on January 24th 2012 | Software
In case, we are selling mass products under the banner of mass marketing, thus, there are no doubts ...
 
Android app development - Points to ponder
Published by Angel on February 14th 2012 | Software
An experienced Android app development team has the potentials to build effective Android apps for v...
 
SEO BY India hire the services of SEO
Published by Manas on January 17th 2012 | Software
SEO India offering unmatched SEO at an affordable cost. We have mastered the art of seo through year...
 
Reverse Osmosis Process with the development of membranes that could be used to desalt water
Published by Jamesarmstrong2011 on July 19th 2012 | Computer
Reverse osmosis is a process whereby a pump is used to pressure water through a membrane, and away f...
 
The Great importance of a third-party Associate in Custom SharePoint development
Published by Randykennedy on June 22nd 2012 | Software
As SharePoint experts, you could be on sides in the case of the SharePoint software. You can do this...
 
Outsourcing Joomla web developer for stress-free web Joomla development
Published by Sam Anderson on August 22nd 2012 | Software
Hire Joomla web developer from reliable outsourcing companies offers stress free hiring processes an...