PDO
Menu PHP Data Objects
 

PDO - PHP Data Objects

In my analysis of data applications on-line and the development of the use of a mySQL database for (hopefully) a site search, I am now aware of PDO and that it might be different to just mySQLi.

Although I only intend to use the database that comes with my hosting I feel that I should use PDO as a matter of principle.

What the PHP manual says about PDO:

" The PHP Data Objects (PDO) extension defines a lightweight, consistent interface for accessing databases in PHP. Each database driver that implements the PDO interface can expose database-specific features as regular extension functions. Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server. "

Top

In addition there is also a distinction between object-oriented and proceedural for mySQLi. PDO looks like it ia Object-Oriented as the code creates an object. The W3C site shows the mySQLi O-O syntax:

<?php
$servername = "localhost"; $username = "username"; $password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>

I was using code similar to the above in my test select,create and update scripts.

The PDO connect syntax:

<?php $servername = "localhost"; $username = "username"; $password = "password";

try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>

PDO Predefined Constants

With PDO there are many Predefined Constants that are used in the command syntax.

ATTR_ERRMODE and ERRMODE_EXCEPTION are examples.

Links

Top

References:

Site design by Tempusfugit Web Design -