IntactDev
Member
- Nov 22, 2012
- 399
- 71
Hello my fellow DevBesters!
Today I will share with you the most useful (imo) interface for accessing databases. Here is what PHP.net says about it.
Step 1:
First, you need to define your database credentials into variables.
Step 2:
Initiate your connect to the database, and check for errors.
Step 3:
Do database functions (like queries!)
I hope you enjoyed the tutorial. If I made any mistakes (and I don't think I did) just let me know and I'll gladly fix/improve them!
Today I will share with you the most useful (imo) interface for accessing databases. Here is what PHP.net says about it.
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 aYou must be registered for see linksto access a database server.
So let's get startedPDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.
PDO ships with PHP 5.1, and is available as a PECL extension for PHP 5.0; PDO requires the new OO features in the core of PHP 5, and so will not run with earlier versions of PHP.
Step 1:
First, you need to define your database credentials into variables.
PHP:
$config['PDO']['Hostname'] = 'localhost'; # Database hostname
$config['PDO']['Username'] = 'root'; # Database Username
$config['PDO']['Password'] = 'password'; # Database Password
$config['PDO']['Database'] = 'db'; # Database name
$config['PDO']['Data']['Source']['Name'] = 'mysql:dbname=' . $config['PDO']['Database'] . ';host='. $config['PDO']['Hostname'] .';';
Step 2:
Initiate your connect to the database, and check for errors.
PHP:
try {
$db = new PDO($config['PDO']['Data']['Source']['Name'], $config['PDO']['Username'], $config['PDO']['Password']); # Connect to the database
} catch(PDOException $e) {
die($e->getMessage()); # if there is an error, the script will end and return the error message.
}
Step 3:
Do database functions (like queries!)
PHP:
$query = $db->prepare('SELECT first_name, last_name, email FROM users ORDER BY first_name ASC'); # Unlike MySQL, with PDO you have to prepare the query first.
$query->execute(); # Then, you simply execute the query.
# Let's say you wanted to fetch all the results, you would do something like this
$result = $query->fetchAll(); # Fetches all results from query.
# Then you could do the following...
echo $result['first_name']; # Returns result of the 'first_name' column for the FIRST result.
I hope you enjoyed the tutorial. If I made any mistakes (and I don't think I did) just let me know and I'll gladly fix/improve them!
Last edited: