Hudzilla.org - the homepage of Paul Hudson
Contents > Databases > PEAR::DB Wish List | Report Bug | About Me ]

9.6.1     Quick PEAR::DB calls

This is NOT the latest copy of this book; click here for the latest version.

PEAR::DB has three special functions that are designed to make very simple SQL queries easy to use from within PHP. These functions are getOne(), getRow(), and getCol(), and each take an SQL query to execute as their parameter. GetOne() executes the query, then returns the first row of the first column of that query, getRow() returns all columns of the first row in the query, and getCol() returns the first column of all rows in the query. GetOne() returns just one value, whereas getRow() and getCol() both return arrays of values.

Here is an example demonstrating each of these functions in action, using the people table created earlier:

<?php
    
include_once('DB.php');
    
$db = DB::connect("mysql://root:alm65z@localhost/phpdb");
    
    if (
DB::isError($db)) {
        print
$db->getMessage();
        exit;
    } else {
        
$maxvisits = $db->getOne("SELECT MAX(NumVisits) FROM people;");
        print
"The highest visit count is $maxvisits<BR />";
        
$allnames = $db->getCol("SELECT Name FROM people;");
        print
implode(', ', $allnames) . '<BR />';
        
$onecol = $db->getRow("SELECT * FROM people WHERE Name = 'Ildiko';");
        
var_dump($onecol);
    }

    
$db->disconnect();
?>

As you can see, using these three "quick access" functions mean you do not need to bother with anything more than just one simple function call.





<< 9.6 PEAR::DB   9.6.2 Query information >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 01 Dec 2008

Perhaps you have a wrong configuration in the web server (it happens to me too).

Good luck!

A PHP User - 01 Dec 2008

Hey

Maybe php doesn't find your pear (I do suppose it's installed, it doesn't come with php, I think... can be downloaded from http://pear.php.net/package/DB/download - just in case).

If there is just a blank page, rather than an error message telling you what causes it (all the blankness :)
I would recommend error reporting:
error_reporting(E_ALL|E_STRICT);

And to tell php explicitly where pear sits:
ini_set('include_path', '/path/to/your/pear/');

Hope this was helpful.
Good luck!

PHP user - 01 Dec 2008

i've installed php normally...
but when i use normal db functions it works but PEAR::DB not working. It is giving blank page..

Any external setting needed for that Plz answer me...
or any configuration for include DB.php!!!!



Send me Reply On::patilsachin4u@rediffmail.com

Sachin - 01 Dec 2008

i've installed php normally...
but when i use normal db functions it works but PEAR::DB not working. It is giving blank page..

Any external setting needed for that Plz answer me...
or any configuration for include DB.php!!!!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow