Quantcast
Channel: stoimen's web log » Data management
Viewing all articles
Browse latest Browse all 2

Fetching Rows With Zend_Db fetch()

$
0
0

Fetching the Entire Row Set

Rows

What is really handy in Zend Framework is that you can fetch the entire row set with the fetchAll() method. It comes with some parameters that you can use for limiting the result or ordering, but in general you can use it without specifying parameters. Let say this is the model:

<?php
class User extends Zend_Db_Table
{
 
	public function listAll()
	{
		$query = "SELECT * FROM user";
 
		// exec query
		$rs = $this->getAdapter()->query($query);
 
		return $rs->fetchAll();
 
	}
 
}

You can simply return the row set with the fetchAll() method as described in the example, but what if you have to loop through the rows and to modify somehow the values?

Fetching a Row

By simply change the code like so:

class User extends Zend_Db_Table
{
 
	public function listAll()
	{
		$query = "SELECT * FROM user";
 
		// exec query
		$rs = $this->getAdapter()->query($query);
 
		// fetch
		$list = array();
		while ($row = $rs->fetch()) {
			// removing the password column value
			$row['password'] = '';
 
		    $list[] = $row;
		}
 
		return $list;
	}
}

you can modify the rows and you’ll have the same result.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images