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:
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:
$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:
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.