672Part IVConnectionsNative database connectivityNative database functions, because they (Web hosting faq)
672Part IVConnectionsNative database connectivityNative database functions, because they are written in C rather than PHP, will always be thefastest way for a PHP script to connect to a database. However, there is a whole series offunctions for each supported database. In a nutshell, the process involves a series of steps, each customized to the database being used. To connect to a MySQL database, a program uses a command like this: mysql_connect( localhost , root , sesame ); Then, to specify which database it is going to work with, the program issues a command like this: mysql_select_db( phpbook ); Then the program defines and issues a query: $query = SELECT Surname FROM personal_info WHERE ID<10 ; $result = mysql_query($query); With that done, it is a matter of looping through the result set to see what came back from thedatabase: while ($name_row = mysql_fetch_row($result)) { print( $name_row[0] $name_row[1] $name_row[2]
n ); } To do the same work under another database management server, four of the six lines wouldhave to be modified. Here s the same code for PostgresSQL (assuming the same hostnameand other details): pg_connect( localhost , root , sesame ); pg_select_db( phpbook ); $query = SELECT Surname FROM personal_info WHERE ID<10 ; $result = pg_query($query); while ($name_row = pg_fetch_row($result)) { print( $name_row[0] $name_row[1] $name_row[2]
n ); } That can be a real hassle, and the process introduces all kinds of opportunities for error. PEAR DB solves this problem. PEAR DB and PHP5At the time of this writing, changes specific to PHP5 had mostly not yet been incorporated intoPEAR DB. All the major changes in PHP5 except XML affect PEAR DB: the new object model, exception handling, SQLite, and the new mysqli extension. Of these, only SQLite is now beinghandled correctly. Expect changes in PEAR DB when these new features trickle down.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.