Linux Systems Administrator
Jupitermedia
US-AZ-Tucson

Justtechjobs.com Post A Job | Post A Resume

Best Practices: Database Abstraction
A PEAR mini-HOWTO
Abstraction is a technique which simplifies something complex. It does this by removing non-essential parts of the object, allowing us to concentrate on the important parts.
In the case of database abstraction, the complexities of connecting to a database is hidden behind a standard API, thereby allowing the programmer to connect to many different types of databases without relearning the methods and syntax peculiar to each different type.
PEAR's DB classes are one such database abstraction layer, and in this article we'll take a look at some traditional database access methods and then compare them with the method employed by PEAR.
Traditional Database Access
Let's quickly run through how you would connect to a database and run through the records using PHP's native functions. We'll start with MySQL since that seems to be one of the more popular databases. Note to Postgresql users - I said _more_ popular, not _most_ popular, so you can hold back on that hate mail for the moment :) [ *cough* -tim ]
Here's an SQL dump for the database that I am using. It's MySQL and the database is called phptst. It consists of a single table called demo which has 3 fields.
# MySQL dump 8.11
#
# Host: localhost	Database: phptst
#--------------------------------------------------------
# Server version	3.23.28-gamma

#
# Table structure for table 'demo'
#

CREATE TABLE demo (
  demo_id mediumint(9) NOT NULL auto_increment,
  demo_stamp bigint(20),
  demo_text varchar(50),
  PRIMARY KEY (demo_id)
);

#
# Dumping data for table 'demo'
#

INSERT INTO demo VALUES (1,978872113,'Record number 1');
INSERT INTO demo VALUES (2,978872652,'Record number 2');
INSERT INTO demo VALUES (3,978872652,'Record number 3');
INSERT INTO demo VALUES (4,978872652,'Record number 4');
INSERT INTO demo VALUES (5,978872652,'Record number 5');
Right, on to the code:

<?php

include( 'dbinfo.php' );
$conn = mysql_connect( $dbhost, $dbuser, $dbpass );
mysql_select_db($dbname);
$sql = 'SELECT * FROM demo';
$demoResult = mysql_query( $sql );
while (
$demoRow = mysql_fetch_row( $demoResult ) ) {
    echo
$demoRow[2] . '<br>';
}
mysql_close ($conn);

?>
The first thing that I like to do is move all of my connection information out of the main script and into a small include file. This way when I change the password for the database I don't have to go and edit every script that connects to that database. My dbinfo.php looks something like this:

<?php

$dbhost
= 'localhost';
$dbuser = 'theuser';
$dbpass = 'thepassword';
$dbname = 'phptst';

?>
The line $conn = mysql_connect( $dbhost, $dbuser, $dbpass ); now uses the variables I set in dbinfo.php rather than having the info hard coded. OK. $conn is now a link identifier to my database connection. mysql_select_db($dbname); tells PHP which MySQL database I want to use.
I set up my query with $sql = 'SELECT * FROM demo'; and then run the query on my database with $demoResult = mysql_query( $sql );. $demoResult is now an idetifier to the result set that my query just returned. We then use a while statement to loop through the result set:
while ( $demoRow = mysql_fetch_row( $demoResult ) ) {
each iteration populating the array $demoRow with next row of information from our result set. When we're done we can close our connection to the database with a call to mysql_close().
Here's the output from that script:
Record number 1
Record number 2
Record number 3
Record number 4
Record number 5
If that quick and dirty run through mysql was too horrible to bear, there's an intro article to PHP and MySQL on webmonkey.
[ Next Page ]


Comments:
php problemeric soh07/27/05 04:46
¡Ú¡Ú<´ë¹Ú>¼ºÀÎ Æ÷¸£³ë100% ¹«·á ½ÎÀÌÆ®¡Ú¡ÚÀÌÁØ¿µ01/29/05 00:02
¡á¡á¾Öµé ¸ô·¡ º¸¼¼¿ä^^¡á¡á ºñ¹Ð12/07/04 12:57
¡á¡á572¸í°ú S.E.X ½Å±â·ÏÀ» ¼¼¿î ¿©.ÀÚ °ø°³¡á¡á ½Å±â·Ï12/07/04 00:54
¡á¡á È­»ó äÆÃÁß XX¸¦ ´Ù º¸¿© Áֳ׿ä.¡á¡á ±èÀº°æ12/06/04 09:32
¹«·á¼ºÀÎ,¿µÈ­,¸¸È­,À¯¸Ó,¿î¼¼ º¸·¯¿À¼¼¿ä¹«·á¸Ç12/05/04 21:04
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø12/05/04 06:54
Ä«,µå,¿¬,ü,ÀÚ/¿¹.Á¤.ÀÚ ´ë,Ãâ 100-1000¸¸¿øÀÌ´ÙÇö12/05/04 02:34
¡á¡áÀý.´ë ¼º.ÀÎ.¸¸ Ŭ¸¯Çϼ¼¿ä.¡á¡á ÀÌÀºÈñ12/04/04 20:31
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í12/04/04 11:46
¡á¡á¡á Çö±ÝÀ» ÁÖ´Â °í½ºÅé Æ÷Ä¿ »çÀÌÆ®¡á¡á¡á °ÔÀÓ12/04/04 00:32
¡á¡á¡á¸ÅÀÏ Çö±ÝÀ» ÁÖ´Â °í½ºÅé Æ÷Ä¿ »çÀÌÆ®¡á¡á¡á ÀÓ°æÀº12/03/04 08:51
### °í½ºÅé Æ÷Ä¿ °ÔÀÓ Çϸé Çö±ÝÀ» µå·Á¿ä ### ÀÌÀº°æ12/02/04 01:16
(¾ßµ¿)2004³â ÃÖ°í È­Á¦ Æ÷¸£³ë¿±±â¸Ç12/02/04 00:46
¢Î¢Ï¢Î¾ß.µ¿.Àá.½Ã.ÈÄ.¿¡ ©.¸³.´Ï´Ù.¢Ï¢Î¢ÏÀÌÀº°æ12/01/04 20:06
¢Î¢Ï¢Î¼º.ÀÎ.¸¸ Ŭ.¸¯ ÇÏ.¼¼¿ä.¢Ï¢Î¢ÏÀÌÀº¹Ì11/30/04 15:48
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/27/04 07:29
##°í½ºÅé Æ÷Ä¿ °ÔÀÓ ÇÏ½Ã¸é ¸ÅÀÏ Çö±ÝÀ» µå¸³´Ï´Ù..##À̼±Èñ11/26/04 12:56
@@¸ÅÀÏ Çö±ÝÀ» ÁÖ´Â °í½ºÅé Æ÷Ä¿ °ÔÀÓ Çϼ¼¿ä.@@¼­ºñ½º11/25/04 18:20
¡Ú¡Ú´ëÇѹα¹ ¿©°í»ý 60%°¡ ÀÚÀ§¸¦ ÇѴٴٳ׿ä¡Ú¡ÚÃæ°Ý11/23/04 11:12
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¹Î11/23/04 07:18
¡Ú¡Ú¹«.·á.¾ß.µ¿¡Ú¡ÚÀÌÀº°æ11/22/04 18:32
½Å.¿ëºÒ.·®ÀÚ°¡ ¾Ë¾Æ¾ßÇÒ Á¤.º¸ ´ë.°ø.°³ ÀÌ´ÙÁø11/21/04 22:21
¡Ú¡Ú¹«.·á.¾ß.µ¿..Àá½ÃÈÄ¿¡ ©¸³´Ï´Ù..¡Ú¡Ú°øÂ¥11/21/04 20:37
¡Ú¡Ú¡ÚºÎ.ºÎ ½º.¿Í.ÇÎ µ¿.¿µ.»ó °ø.°³¡Ú¡Ú¡ÚÇöÀå°ø°³11/21/04 06:21
[´ÙÀÌ¾îÆ®Á¤º¸]³¯¾ÀÇØÁö·Á¸é Äá ¸¹ÀÌ ¸Ô¾î¾ß´ÙÀÌ¾îÆ®11/20/04 20:21
¡Ú¡Ú¡ÚºÎ.ºÎ ½º.¿Í.ÇÎ µ¿.¿µ.»ó °ø.°³¡Ú¡Ú¡ÚÇöÀå°ø°³11/20/04 05:57
¡Ú¡Ú¡Ú½ÇÁ¦»ðÀÔ Çѱ¹ ºüX¸® µ¿¿µ»ó!!¡Ú¡Ú¡Ú¼îÅ·11/18/04 12:54
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÁ¤¿¬11/18/04 10:20
$$$ °í½ºÅé Æ÷Ä¿ »çÀÌÆ®¿¡¼­ ¸ÅÀÏ Çö±Ý Áö±Þ $$$À̹̰æ11/17/04 17:07
<°­Ãß>¿±±â °­°£ ¸ôÄ« ÀϺ»Æ÷¸£³ë ¸ðÀ½Áý¼º±¤11/15/04 20:23
$$$°í½ºÅé Æ÷Ä¿ °ÔÀÓ Çϸé Çö±ÝÀ» Áֳ׿ä..$$$´ë¹Ú°ÔÀÓ11/14/04 01:44
+++++ Çѱ¹ Æ÷¸£³ë ¸ôÄ« ´Ù¿î·Îµå+++++¼­Àαâ11/13/04 04:16
About the CodingMuthukumar05/13/04 04:04
¢Ñ¢Ñ Ä¿Çà ¿Á»ó¿¡¼­ ccTV ¿¡ ÀâÇûÀ½µ¿¿µ»ó10/30/03 09:20
½Å¿ëºÒ·®/Ä«µå¿¬Ã¼/È¥ÀÚÇØ°áÇÏ´Â ¹æ¹ý!!(Çʵ¶) µµ¿ì¹Ì09/29/03 23:47
½Å¿ëºÒ·®/Ä«µå¿¬Ã¼/È¥ÀÚÇØ°áÇÏ´Â ¹æ¹ý!!(Çʵ¶) µµ¿ì¹Ì09/28/03 01:42
¢Ñ¢Ñ ip ÃßÀû ¢Ð¢Ð¼Ò½º¸¶´ç09/26/03 16:26
Request of a topicmalik sohail 11/05/02 11:02
about computerization and data basejeffree rugare09/23/02 10:43
RE: Advantages over PHPLib?Manish09/23/02 08:22
How can i develop web portal Muhammad Adeel Khan09/15/02 14:59
how do you say "if query returns no results"?Katherine08/20/02 12:19
free+MySQL+hostsdan08/16/02 04:18
parsing a file to an sql databasesfad08/13/02 00:46
Connectinc MS - Access Through The PHPJaved Mansuri08/04/02 03:40
Database Abstraction & PEARKendall07/30/02 14:09
RE: Pear docs/workTim Parkin06/10/02 06:48
RE: sweet heartnikhil pant05/08/02 02:32
RE: url from a string in a databaseLucas Vieites04/19/02 04:46
Datatable Transfersurender04/06/02 14:35
sweet heartPAKISTAN INSTITUTE OF TECHNOLOGY03/18/02 07:35
Error checkingMark03/07/02 13:29
RE: mysql_field_name ? in Pear function ?Brian Uniacke02/21/02 08:11
mysql_field_name ? in Pear function ?Terry02/19/02 22:18
ASP components translated for PHP...jasprogrammer01/21/02 01:30
connecting the database to my sql in phpmalbro_indian12/24/01 00:44
RE: PEAR ModificationBashar10/06/01 09:16
How to access PEAR::DB from abstract class? Chad Nantais09/12/01 01:06
url from a string in a databasejock09/05/01 11:25
PEAR and InterbaseMartin Zingor07/26/01 23:19
What about PEAR and Interbase?Martin Zingor07/19/01 22:52
RE: Advantages over PHPLib?Don Dudles07/19/01 08:57
The Dumbed Down Go Hi-techMike Warner06/23/01 18:30
RE: PEAR ModificationDanny Appaiah06/14/01 01:52
RE: Where to find a good reference manual philip olson05/08/01 11:13
another tutorialphilip olson04/26/01 03:15
PEAR ModificationPiLoT & Jazze03/15/01 13:13
RE: Where to find a good reference manual bmpc02/27/01 12:59
Where to find a good reference manual Jazze02/26/01 02:43
RE: PHP & MS SQLServer 7 SP3POURE02/20/01 01:17
RE: Database Abstraction a White Elephantmdeadly02/17/01 07:34
RE: Database Abstraction a White ElephantDavid02/13/01 18:04
radio value retrivationsubbu02/08/01 08:33
PHP & MS SQLServer 7 SP3José Abranches02/07/01 05:14
RE: Advantages over PHPLib?dennis iversen02/05/01 23:17
PEAR connect() questionEnrique Villar02/01/01 07:41
RE: Database Abstraction a White ElephantEldon01/31/01 17:59
webmailozy01/31/01 02:13
Pear docs/workMelvyn Sopacua01/27/01 21:33
Pear ExamplesGary Laxton01/24/01 12:57
RE: where to get this class.stig01/24/01 09:52
RE: Database Abstraction a White ElephantKen01/23/01 01:33
RE: What about xxx_fetch_object()wizactive01/22/01 02:46
Database Abstraction a White ElephantTim Parkin01/20/01 08:21
RE: where to get this class.Andreas Heintze01/20/01 05:24
PHPLib and PEARRichard Heyes01/18/01 06:25
ADODB-good if you use PHP & ASPJohn Lim01/17/01 12:53
RE: Advantages over PHPLib?Javier Leyba01/17/01 09:20
RE: Advantages over PHPLib?lizo01/17/01 04:17
RE: where to get this class.Joshua Eichorn01/16/01 12:12
RE: Advantages over PHPLib?Jeroen01/16/01 04:50
What about xxx_fetch_object()Petar Pavlovic01/16/01 04:31
RE: What about ADODB?Ronald TAO01/16/01 04:20
RE: Advantages over PHPLib?Kirk Parker01/16/01 02:11
RE: Advantages over PHPLib?Robert Treat01/15/01 22:49
What about ADODB?Tim Uckun01/15/01 18:47
where to get this class.philip olson01/15/01 15:26
RE: Advantages over PHPLib?Viking01/15/01 14:05
class = nice. PEAR = not done.Viking01/15/01 13:36
Perl library as opposed to PHP specificJeremy Rempel01/15/01 13:06
Advantages over PHPLib?Peter01/15/01 13:06
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.