Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Best Practices: Database Abstraction
Doing it the PEAR way
Out with the old, and in with the new - how the hell do we do that with PEAR ? Here's our MySQL example above, rewritten using PEAR DB.

<?php

    
include('dbinfo.php');
    require_once(
'DB.php' );
    
$db = DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );
    
$sql = 'SELECT * FROM demo';
    
$demoResult = $db->query($sql);
    while (
$demoRow = $demoResult->fetchRow()) {
        echo
$demoRow[2] . '<br>';
    }
    
$db->disconnect();

?>
OK, compare that to what we saw with MySQL. The process is pretty much the same (connect, query, loop through result, disconnect), but the way that we do each step has changed. Let's go through it line by line and I'll explain what's going on.
Our first line is as it was in our original script. I'm including my dbinfo.php so that I don't have to hard code my database usernames and passwords. The next line require_once( "DB.php" ); is require'ing the file DB.php. The _once part makes sure that requires of files that contain duplicate variable or function declarations do not break your script.
The file DB.php can be found in the /pear subdirectory of the PHP4 distribution. If the installation process didn't do it, make sure that the directory that contains the PEAR files is included in your include_path in the php.ini.
Inside DB.php a class of type DB is defined. One of the functions of the DB class is the connect() function, and this is what we are using here:

<?php

$db
= DB::connect( "mysql://$dbuser:$dbpass@$dbhost/$dbname" );

?>
The connect() function creates a new database object and connects to the database as specified in the string that you pass it. The string takes the format of:
phptype://username:password@protocol+hostspec/database
phptype is the type of database that you wish to connect to. Valid types are ibase, msql, mssql, mysql, oci8, odbc, pgsql and sybase.
username, password and hostspec are self explanatory. protocol is the protocol to connect on (eg tcp) and database is the database that you want to use. The only thing in that string that is required is the phptype. Everything else is optional, but in most cases you are going to want to specify a username, password and database.
If I wasn't using the dbinfo.php include file the string I passed the connect function would look something like:
"mysql://theuser:thepassword@localhost/phptst"
Assuming that the connect() function succeeded, $db is now an object that not only contains methods for accessing your database but also contains all sorts of information about the conection you have just created.
What is important for this example though, is that the object has a function called query() that will execute an SQL statement on the database. It returns a result object with it's own properties and methods that we can use to get at the data.
$demoResult = $db->query($sql);
says "in the object $db is a function called query(). Run it with the argument $sql and stick the results you get back from it in $demoResult".
One of the functions in $demoResult is fetchRow(). fetchRow() will return a row of data as an array, much like mysql_fetch_row() will.
while ($demoRow = $demoResult->fetchRow()) {
fetchRow() is returning an array indexed numerically. What if you wanted to get an associative array back like you can with mysql_fetch_assoc() ? The fetchRow() function has an optional argument that specifies the type of array that will be returned by the function. If no argument is specified then DB_FETCHMODE_DEFAULT is assumed. It then returns whatever the efault has been set to. If you wanted to explicitly specify each one, then you would use DB_FETCHMODE_ORDERED for an array index numerically, or DB_FETCHMODE_ASSOC for an array index by field name (associative). DB_FETCHMODE_DEFAULT, DB_FETCHMODE_ORDERED and DB_FETCHMODE_ASSOC are all constants that are defined in DB.php.
So if we wanted to use an associative array, we would rewrite our code as follows:

<?php

while ($demoRow = $demoResult->fetchRow(DB_FETCHMODE_ASSOC)) {
    echo
$demoRow['demo_text'] . '<br>';
}

?>
Once we have finished with our connection to the database we can disconnect with $db->disconnect();.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
help me in my php languagebenjamin10/20/08 15:12
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.