PDO (PHP Data Objects) is a PHP extension for accessing databases in PHP.
PDO uses OOP characteristics (Object Oriented Programming) available since PHP 5.1.
Because PDO is working with classes and objects, you must be familiar with the PHP object-oriented programming.
PDO can work with the following databases:
foreach(PDO::getAvailableDrivers() as $driver) { echo $driver.'<br />'; }
Any interaction with a database begins by connecting to it. Depending on the type of database, the connection is made by creating an instance object, with: new PDO(). After the connection is created, we apply PDO methods to get and process data. These methods are largely the same, regardless of the connected database. Here is how to connect to some of these database:
<?php // Connection data (server_address, database, username, password) $hostdb = 'localhost'; $namedb = 'dbname'; $userdb = 'username'; $passdb = 'password'; // Display message if successfully connect, otherwise retains and outputs the potential error try { $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); echo 'Connected to database'; } catch(PDOException $e) { echo $e->getMessage(); } ?>- The $conn variable is the PDO object that will contain the data received from MySQL server. The PDO methods for processing data will be aplied to this object.
<?php // Connection data (server_address, database, username, password) $hostdb = 'localhost'; $namedb = 'dbname'; $userdb = 'username'; $passdb = 'password'; $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); ?>- The error message would display more data, including password, as shown below:
<?php try { $conn = new PDO("pgsql:host=localhost port=5432 dbname=pdo", "username", "password"); echo "PDO connection object created"; } catch(PDOException $e) { echo $e->getMessage(); } ?>
<?php try { // connect to SQLite database $conn = new PDO("sqlite:/path/to/database.sdb"); } catch(PDOException $e) { echo $e->getMessage(); } ?>
<?php try { $conn = new PDO("firebird:dbname=localhost:C:\Programs\Firebird\DATABASE.FDB", "SYSDBA", "masterkey"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php try { $conn = new PDO("informix:DSN=InformixDB", "username", "password"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php try { $conn = new PDO("OCI:dbname=accounts;charset=UTF-8", "username", "password") } catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php try { $conn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php $hostdb = "localhost"; $port = 10060; $namedb = "my_database"; $userdb = "username"; $passdb = "password"; try { $conn = new PDO ("dblib:host=$hostdb:$port;dbname=$namedb","$userdb","$passdb"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
<?php try { $conn = new PDO("ibm:DRIVER={IBM DB2 ODBC DRIVER};DATABASE=accounts; HOSTNAME=1.2.3,4;PORT=56789;PROTOCOL=TCPIP;", "username", "password"); } catch (PDOException $e) { echo $e->getMessage(); } ?>
Normally, PHP closes the connection to the database after the script was executed. But the disconnection can be done intentionally, attributing the null value to the PDO object, as it's shown in the example below.
The instance of that object will be destroyed, so, this method should be added after all the instructions at the PDO object were written.
-Disconnecting with this method is useful because it releases the memory used.
<?php // Connection data (server_address, database, username, password) $hostdb = 'localhost'; $namedb = 'dbname'; $userdb = 'username'; $passdb = 'password'; // Display message if successfully connect, otherwise retains and outputs the potential error try { $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb); echo 'Connected to database'; /* Instructions for working with $conn object */ $conn = null; // Close the connection } catch(PDOException $e) { echo $e->getMessage(); } ?>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net