There is another process that you can use to access database information. It is a database extension called PDO (PHP Data Objects), and the php.net web site had this to say about it:
This new product addition and enhancement was scheduled for release in Version 5.1 and should be in general use by the time you are reading this. Basically, this is another approach to connecting to databases in an abstract way. Though similar to the PEAR::DB approach that we have just covered, it has (among others) these unique features:
Since there are a number of features here, we will only touch on a few of them to show you just how beneficial PDO is purported to be. First, a little about PDO. It will have drivers for almost all database engines in existence, and those drivers that PDO does not supply should be accessible through PDO's generic ODBC connection. PDO is modular in that it has to have at least two extensions enabled to be active: the PDO extension itself and the PDO extension specific to the database to which you will be interfacing. See the online documentation to set up the connections for the database of your choice at http://ca.php.net/pdo. For establishing PDO on a windows server for MySQL interaction, simply enter the following two lines into your php.ini file and restart your server: extension=php_pdo.dll The PDO library is also an object-oriented extension (you will see this in the code examples that follow). Making a connection The first thing that is required for PDO is that you make a connection to the database in question and hold that connection in a connection handle variable, as in the following code: $ConnHandle = new PDO ($dsn, $username, $password); The$dsn stands for the data source name, and the other two parameters are self-explanatory. Specifically, for a MySQL connection, you would write the following code: $ConnHandle = new PDO('mysql:host=localhost;dbname=library', Of course, you could (should) maintain the username and password parameters as variable-based for code reuse and flexibility reasons. Interaction with the database So, once you have the connection to your database engine and the database that you want to interact with, you can use that connection to send SQL commands to the server. A simple UPDATE statement would look like this: $ConnHandle->query("UPDATE books SET authorid=4 " This code simply updates the books table and releases the query. This is how you would usually send non-resulting simple SQL commands (UPDATE, DELETE, INSERT) to the database through PDO, unless you are using prepared statements, a more complex approach that is discussed in the next section.
blog comments powered by Disqus |