Set WeberTrivia.com to be my default homepage.   Suggest a Question                                               

Suggest A Question : :  Frequently Asked Questions : :  Search : :  Relevant Manuals : : 
PHP Questions : :  Linux Questions : :  MySQL Questions : : 
home  [ Login ] 

Introduction - Fetch & Get

Introduction - Fetch & Get -- Fetching rows from queries

Description

Choosing Formats for Fetched Rows

The fetch modes supported are:

Fetch Rows by Number

The PEAR DB fetch system also supports an extra parameter to the fetch statement. So you can fetch rows from a result by number. This is especially helpful if you only want to show sets of an entire result (for example in building paginated HTML lists), fetch rows in an special order, etc.

Quick Data Retrieving

DB provides some special ways to retrieve information from a query without the need of using fetch*() and looping through results.

getOne() retrieves the first result of the first column from a query

<?php // Once you have a valid DB object named $db... $numrows = $db->getOne('select count(id) from clients'); ?>

getRow() returns the first row as an array.

<?php // Once you have a valid DB object named $db... $sql = 'select name, address from clients where id=1'; while ($row =& $db->getRow($sql)) {     echo $row['name'] . ', ' . $row['address'] . "\n"; } ?>

getCol() returns an array with the data of the selected column. It accepts the column number to retrieve as the second parameter.

<?php // Once you have a valid DB object named $db... $data =& $db->getCol('SELECT name FROM clients');  /*  * Will return:  * array(  *     0 => 'Stig',  *     1 => 'Jon',  *     2 => 'Colin'  * )  */ ?>

getAssoc() fetches the entire result set of a query and return it as an associative array using the first column as the key.

<?php // Once you have a valid DB object named $db... $data =& $db->getAssoc('SELECT name, surname, phone FROM mytable'); /*  * Will return:  * array(  *     'Peter'   => array('Smith', '944679408'),  *     'Tomas'   => array('Cox', '944679408'),  *     'Richard' => array('Merz', '944679408')  * )  */ ?>

getAll() fetches all the rows returned from a query-

<?php // Once you have a valid DB object named $db... $data =& $db->getAll('SELECT id, text, date FROM mytable'); /*  * Will return:  * array(  *     0 => array('4', 'four', '2004'),  *     1 => array('5', 'five', '2005'),  *     2 => array('6', 'six', '2006')  * )  */ ?>

The get*() family methods will do all the dirty job for you, this is: launch the query, fetch the data and free the result. Please note that as all PEAR DB functions they will return a DB_Error object on errors.

Getting More Information From Query Results

With DB you have many ways to retrieve useful information from query results. These are:

  • numRows(): Returns the total number of rows returned from a SELECT query.

    <?php // Once you have a valid DB object named $db... $res =& $db->query('SELECT * FROM phptest'); echo $res->numRows(); ?>

  • numCols(): Returns the total number of columns returned from a SELECT query.

    <?php // Once you have a valid DB object named $db... $res =& $db->query('SELECT * FROM phptest'); echo $res->numCols(); ?>

  • affectedRows(): Returns the number of rows affected by a data manipulation query (INSERT, UPDATE or DELETE).

    <?php // remember that this statement won't return a result object $db->query('DELETE * FROM clients'); echo 'I have deleted ' . $db->affectedRows() . ' clients'; ?>

  • tableInfo(): Returns an associative array with information about the returned fields from a SELECT query.

    <?php // Once you have a valid DB object named $db... $res =& $db->query('SELECT * FROM phptest'); print_r($db->tableInfo($res)); ?>

Don't forget to check if the returned result from your action is a DB_Error object. If you get a error message like "DB_Error: database not capable", means that your database backend doesn't support this action.

Who's Online
Guest Users: 7
Google
Web
WeberTrivia
WeberDev
WeberForums
 Free Sample Chapters  Free Sample Chapters
  Deliver First Class Web Sites: 101 Essential Checklists
Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?

More Sample Chapters

PHP General