<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL keywords &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/sql-keywords/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Computer Algorithms: Linked List</title>
		<link>/2012/06/14/computer-algorithms-linked-list-data-structure/</link>
		<comments>/2012/06/14/computer-algorithms-linked-list-data-structure/#comments</comments>
		<pubDate>Thu, 14 Jun 2012 12:12:47 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[data structures]]></category>
		<category><![CDATA[B-tree]]></category>
		<category><![CDATA[Bin]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Hash table]]></category>
		<category><![CDATA[HEAD]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[John]]></category>
		<category><![CDATA[Linked list]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Null]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Pointer]]></category>
		<category><![CDATA[public $head]]></category>
		<category><![CDATA[Smith]]></category>
		<category><![CDATA[SQL keywords]]></category>
		<category><![CDATA[Stack]]></category>

		<guid isPermaLink="false">/?p=3188</guid>
		<description><![CDATA[Introduction The linked list is a data structure in which the items are ordered in a linear way. Although modern programming languages support very flexible and rich libraries that works with arrays, which use arrays to represent lists, the principles of building a linked list remain very important. The way linked lists are implemented is &#8230; <a href="/2012/06/14/computer-algorithms-linked-list-data-structure/" class="more-link">Continue reading <span class="screen-reader-text">Computer Algorithms: Linked List</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2012/06/05/computer-algorithms-stack-and-queue-data-structure/" rel="bookmark" title="Computer Algorithms: Stack and Queue">Computer Algorithms: Stack and Queue </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The linked list is a data structure in which the items are ordered in a linear way. Although modern programming languages support very flexible and rich libraries that works with arrays, which use arrays to represent lists, the principles of building a linked list remain very important. The way linked lists are implemented is a ground level in order to build more complex data structures such as trees. </p>
<p>It&#8217;s true that almost every operation we can perform on a linked list can be done with an array. It&#8217;s also true that some operations can be faster on arrays compared to linked lists. </p>
<p>However understanding how to implement the basic operations of a linked list such as INSERT, DELETE, INSERT_AFTER, PRINT and so on is crucial in order to implement data structures as rooted trees, B-trees, red-black trees, etc.</p>
<h2>Overview</h2>
<p>Unlike arrays where we don’t have pointers to the next and the previous item, the linked list is designed to support such pointers. In some implementations there is only one pointer pointing to the successor of the item. This kind of data structures are called singly linked lists. In this case the the last element doesn’t have a successor, so the pointer to its next element usualy is NULL. However the most implemented version of a linked list supports two pointers. These are the so called doubly linked lists.</p>
<p><figure id="attachment_3197" style="width: 621px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list.png"><img src="/wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list.png" alt="Arrays vs. linked list" title="Arrays vs. linked list" width="621" height="333" class="size-full wp-image-3197" srcset="/wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list.png 621w, /wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list-300x160.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></a><figcaption class="wp-caption-text">Arrays items are defined by their indices, while the linked list item contains a pointer to its predecessor and his successor!</figcaption></figure><span id="more-3188"></span></p>
<p>Let’s take a look at some examples. Here’s an array:</p>
<pre lang="PHP">
A[2, 'hello', 'world', 13, 31]
</pre>
<p>We know that A[0] contains the number 2, while A[1] contains the word (string) &#8220;hello&#8221;. This is the very basic array representation where the indices are consecutive and start from 0.</p>
<p>In that case we know that if we’re looking at the item with index <strong>i</strong>, its next element has an index <strong>i+1</strong>, while his predecessor&#8217;s index is <strong>i-1</strong>. Thus we can easily iterate over items. However if we have an array with only 5 elements A[5] doesn’t exists, and in most of the cases is NULL. In other words array items are defined by their indices, and can be accessed directly with their index.</p>
<p>In the other hand most of the programming languages support associative arrays also called hash maps.</p>
<p>An associative array in PHP can be something like this.</p>
<pre lang="PHP">
$a = array(
	'hello' => 'world',
	31	=> 12,
	'b7'	=> 144,
);
</pre>
<p>Now because the indices are not consecutive integers we don’t know the successor and the predecessor of the current item. Yes, there is some internal pointer that can give us the link to the previous and the next item, but explicitly we can&#8217;t refer them.</p>
<p>Of course some libraries may have functions that can point us to the next item, such a function is <a href="http://php.net/manual/en/function.next.php" target="_blank">next()</a> in <a href="/category/php/">PHP</a>.</p>
<p>As we see arrays can replace the typical pointer-like representation of a linked list, but as I said already this data structure is crucial in order to understand more complex data structures.</p>
<p>Typically the linked list is implemented with two classes. The first one describing the item and the other one containing the main operations of the list such as search, insert, delete, etc.</p>
<p>The first class, typically called <strong>Item</strong> is a normal class containing various info about an item. In terms of persons, for instance, this class can contain the first and last names of the person, his address, phone etc. However it’s very important that this class contains pointers to its previous and its next objects.</p>
<p>In our terms each object of class Person will contain a pointer to a next object of the same class Person.</p>
<pre lang="PHP">
class Person
{
	public $next = null;
	public $prev = null;
	
	protected $_firstName = '';
	protected $_lastName = '';
	protected $_phone = '';
	
	
	public function __construct($firstName, $lastName, $phone)
	{
		$this->_firstName = $firstName;
		$this->_lastName = $lastName;
		$this->_phone = $phone;
	}
	
	public function __toString()
	{
		return 'First name: ' . $this->_firstName 
			 . ', Last name: ' . $this->_lastName 
			 . ', Phone: ' . $this->_phone;
	}
}
</pre>
<p>So we must first design the structure of an item. </p>
<p>The following thing to do is to define a linked list as an abstraction over a real world list, just like the stack and the queue. We can implement various operations for a linked list like <strong>insert</strong>, <strong>delete</strong>, <strong>insertBefore</strong>, <strong>insertAfter</strong>, <strong>sort</strong>, <strong>search</strong>, <strong>insertSorted</strong> etc. It’s up to the developer to decide which operations he wants to use. And of course this depends on the application.</p>
<h2>Implementation</h2>
<h3>Operations</h3>
<p>We can perform and define as much operations as we need. For instance we can code a linked list only with the basic insert, delete and print, but we can also go for insertBefore, insertAfter, deleteBefore, deleteAfter, insertSorted, which are described on the diagrams below.</p>
<h4>Insert</h4>
<p>Inserting in the front of a list seems much like inserting into a queue. In this case the new item becomes the head of the list and its successor is the previous head of the list.</p>
<figure id="attachment_3204" style="width: 619px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/1.-Insert-at-the-front-of-a-Linked-List.png"><img src="/wp-content/uploads/2012/06/1.-Insert-at-the-front-of-a-Linked-List.png" alt="Inserting at the front of a Linked List" title="Inserting at the front of a Linked List" width="619" height="346" class="size-full wp-image-3204" srcset="/wp-content/uploads/2012/06/1.-Insert-at-the-front-of-a-Linked-List.png 619w, /wp-content/uploads/2012/06/1.-Insert-at-the-front-of-a-Linked-List-300x167.png 300w" sizes="(max-width: 619px) 100vw, 619px" /></a><figcaption class="wp-caption-text">Inserting at the front of the list</figcaption></figure>
<pre lang="PHP">
class LList
{
	public $head;
	public $tail;
	
	public function insert($item) 
	{
		$item->next = $this->head;
		
		if ($this->head != null) {
			$this->head->prev = $item;
		}
		
		$this->head = $item;
	}

	public function __toString()
	{
		$cur = $this->head;
		
		$output = '';
		while ($cur) {
			$output .= $cur . ' ';
			
			$cur = $cur->next;
		}
		
		return $output . "\n";
	}
}

$ll = new LList();

$a = new Person('John', 'Smith', '555 9401');
$b = new Person('James', 'Johnes', '555 2454');

$ll->insert($a);
$ll->insert($b);

// James Johnes 555 2454
// John Smith 555 9401
echo $ll;
</pre>
<h4>Delete</h4>
<p>Delete the head of the list is much like deleting an item from a queue. However we can implement also deleting a custom element that&#8217;s inside the list.</p>
<figure id="attachment_3206" style="width: 618px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/4.-Delete-an-item.png"><img src="/wp-content/uploads/2012/06/4.-Delete-an-item.png" alt="Delete an item" title="Delete an item" width="618" height="362" class="size-full wp-image-3206" srcset="/wp-content/uploads/2012/06/4.-Delete-an-item.png 618w, /wp-content/uploads/2012/06/4.-Delete-an-item-300x175.png 300w" sizes="(max-width: 618px) 100vw, 618px" /></a><figcaption class="wp-caption-text">Deleting an item from the inside of the list!</figcaption></figure>
<pre lang="PHP">
class LList
{
	public $head;
	public $tail;
	
	public function insert($item) 
	{
		$item->next = $this->head;
		
		if ($this->head != null) {
			$this->head->prev = $item;
		}
		
		$this->head = $item;
	}
	
	public function delete($item) 
	{
		$cur = $this->head;
		
		while ($cur) {
			
			if ($cur == $item) {
				$prev = $cur->prev;
				$next = $cur->next;
				
				if ($prev != null) {
					$prev->next = $next;
				} else {
					// because head points to the first item
					$this->head = $next;
				}
				
				if ($next != null) {
					$next->prev = $prev;
				}
				
				return $cur;
			}
		
			$cur = $cur->next;
		}
	}

	public function __toString()
	{
		$cur = $this->head;
		
		$output = '';
		while ($cur) {
			$output .= $cur . ' ';
			
			$cur = $cur->next;
		}
		
		return $output . "\n";
	}
}

$ll = new LList();

$a = new Person('John', 'Smith', '555 9401');
$b = new Person('James', 'Johnes', '555 2454');
$c = new Person('Jeanne', 'Francois', '333 2323');

$ll->insert($a);
$ll->insert($b);
$ll->insert($c);

// prints objects $c, $b, $a:
// Jeanne Francois 333 2323
// James Johnes 555 2454
// John Smith 555 9401
echo $ll;

$ll->delete($b);

// prints objects $c, $a:
// Jeanne Francois 333 2323
// John Smith 555 9401
echo $ll;
</pre>
<h4>Insert before &#038; insert after</h4>
<p>Inserting before and after needs a reference to an already existing list item. After that the new object is inserted into it right place, as shown on the images below.</p>
<figure id="attachment_3208" style="width: 618px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/2.-Insert-after-a-given-item-of-a-Linked-List.png"><img src="/wp-content/uploads/2012/06/2.-Insert-after-a-given-item-of-a-Linked-List.png" alt="Insert after a given item of a Linked List" title="Insert after a given item of a Linked List" width="618" height="362" class="size-full wp-image-3208" srcset="/wp-content/uploads/2012/06/2.-Insert-after-a-given-item-of-a-Linked-List.png 618w, /wp-content/uploads/2012/06/2.-Insert-after-a-given-item-of-a-Linked-List-300x175.png 300w" sizes="(max-width: 618px) 100vw, 618px" /></a><figcaption class="wp-caption-text">Insert after splits the list after the pointed item and inserts the new object there!</figcaption></figure>
<pre lang="PHP">
class LList
{
	public $head;
	public $tail;
	
	public function insert($item) 
	{
		$item->next = $this->head;
		
		if ($this->head != null) {
			$this->head->prev = $item;
		}
		
		$this->head = $item;
	}
	
	public function insertAfter($newItem, $item) 
	{
		$cur = $this->head;
		while ($cur) {
			if ($cur == $item) {
				$next = $cur->next;
				
				$cur->next = $newItem;
				$newItem->prev = $cur;
				
				if ($next != null) {
					$newItem->next = $next;
					$next->prev = $newItem;
				}
				return;
			}
			$cur = $cur->next;
		}
		
		$this->head = $this->tail = $item;
	}

	public function __toString()
	{
		$cur = $this->head;
		
		$output = '';
		while ($cur) {
			$output .= $cur . ' ';
			
			$cur = $cur->next;
		}
		
		return $output . "\n";
	}
}

$ll = new LList();

$a = new Person('John', 'Smith', '555 9401');
$b = new Person('James', 'Johnes', '555 2454');
$c = new Person('Jeanne', 'Francois', '333 2323');

$ll->insert($a);
$ll->insert($c);

// inserts $b after $a
$ll->insertAfter($b, $c);

// Jeanne Francois 333 2323
// James Johnes 555 2454
// John Smith 555 9401
echo $ll;
</pre>
<p>Insert a new object before an item is the same as insert after, but instead of splitting the list after the given item, we split it before it.</p>
<figure id="attachment_3209" style="width: 618px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/3.-Insert-before-a-given-item-of-a-Linked-List.png"><img src="/wp-content/uploads/2012/06/3.-Insert-before-a-given-item-of-a-Linked-List.png" alt="Insert before a given item of a Linked List" title="Insert before a given item of a Linked List" width="618" height="362" class="size-full wp-image-3209" srcset="/wp-content/uploads/2012/06/3.-Insert-before-a-given-item-of-a-Linked-List.png 618w, /wp-content/uploads/2012/06/3.-Insert-before-a-given-item-of-a-Linked-List-300x175.png 300w" sizes="(max-width: 618px) 100vw, 618px" /></a><figcaption class="wp-caption-text">Insert before and insert after are very similar operations!</figcaption></figure>
<h4>Search</h4>
<p>Searching into a list can be modified depending on the application needs. Here we just compare the searched object with a list item.</p>
<a href="/wp-content/uploads/2012/06/5.-Search-for-an-item.png"><img src="/wp-content/uploads/2012/06/5.-Search-for-an-item.png" alt="Search for an item" title="Search for an item" width="620" height="258" class="size-full wp-image-3210" srcset="/wp-content/uploads/2012/06/5.-Search-for-an-item.png 620w, /wp-content/uploads/2012/06/5.-Search-for-an-item-300x124.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a>
<pre lang="PHP">
class LList
{
	public $head;
	public $tail;
	
	public function insert($item) 
	{
		$item->next = $this->head;
		
		if ($this->head != null) {
			$this->head->prev = $item;
		}
		
		$this->head = $item;
	}
	
	public function insertAfter($item, $key) 
	{
		$cur = $this->head;
		while ($cur) {
			if ($cur->data == $key) {
				$next = $cur->next;
				
				$cur->next = $item;
				$item->prev = $cur;
				
				if ($next != null) {
					$item->next = $next;
					$next->prev = $item;
				}
				return;
			}
			$cur = $cur->next;
		}
		
		$this->head = $this->tail = $item;
	}

	public function search($item)
	{
		$cur = $this->head;
		
		while ($cur) {
			if ($cur == $item) {
				return 1;
			}
			
			$cur = $cur->next;
		}
		
		return 0;
	}
	
	public function __toString()
	{
		$cur = $this->head;
		
		$output = '';
		while ($cur) {
			$output .= $cur . ' ';
			
			$cur = $cur->next;
		}
		
		return $output . "\n";
	}
}

$ll = new LList();

$a = new Person('John', 'Smith', '555 9401');
$b = new Person('James', 'Johnes', '555 2454');
$c = new Person('Jeanne', 'Francois', '333 2323');

$ll->insert($a);
$ll->insert($b);

// 1, because $b is in the list
echo $ll->search($b);

// 0, cause $c isn't in the list
echo $ll->search($c);
</pre>
<p>The operations here are only one sub-set of all possible operations you can implement on a linked list. It all depends on our needs. However the principles are important, so we can implement more complex data structures.</p>
<h2>Application</h2>
<p>It&#8217;s true that linked list are a very basic data structure. Someone may wonder why we should code and implement linked list since we can do the same (with almost every modern programming language/library) with arrays. The answer is that data structures as trees are difficult to implement with arrays, so it&#8217;s easy to code them using pointers. Thus linked lists is a ground level for understanding them. Indeed each linked list is a tree with only one branch as shown on the image below.</p>
<figure id="attachment_3213" style="width: 620px" class="wp-caption alignnone"><a href="/wp-content/uploads/2012/06/6.-Linked-List-Trees.png"><img src="/wp-content/uploads/2012/06/6.-Linked-List-Trees.png" alt="Linked List &amp; Trees" title="Linked List &amp; Trees" width="620" height="399" class="size-full wp-image-3213" srcset="/wp-content/uploads/2012/06/6.-Linked-List-Trees.png 620w, /wp-content/uploads/2012/06/6.-Linked-List-Trees-300x193.png 300w" sizes="(max-width: 620px) 100vw, 620px" /></a><figcaption class="wp-caption-text">Each linked list is a single branch tree!</figcaption></figure>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2010/09/29/construct-a-sorted-php-linked-list/" rel="bookmark" title="Construct a Sorted PHP Linked List">Construct a Sorted PHP Linked List </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2012/06/05/computer-algorithms-stack-and-queue-data-structure/" rel="bookmark" title="Computer Algorithms: Stack and Queue">Computer Algorithms: Stack and Queue </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/06/14/computer-algorithms-linked-list-data-structure/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Dump the Generated Zend_Db SQL Query</title>
		<link>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/</link>
		<comments>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 14:39:48 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[Cross-platform software]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Insert]]></category>
		<category><![CDATA[PHP programmer]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>

		<guid isPermaLink="false">/?p=2709</guid>
		<description><![CDATA[The Typical PHP Approach Typically a PHP programmer will write his SQL query as a string and will execute it via mysql_query. $sql = "SELECT * FROM my_table"; $resource = mysql_query($sql); So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its &#8230; <a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" class="more-link">Continue reading <span class="screen-reader-text">How to Dump the Generated Zend_Db SQL Query</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>The Typical PHP Approach</h2>
<p>Typically a PHP programmer will write his SQL query as a string and will execute it via <a href="http://php.net/manual/en/function.mysql-query.php" title="PHP: mysql_query" target="_blank">mysql_query</a>.</p>
<pre lang="PHP">
$sql = "SELECT * FROM my_table";
$resource = mysql_query($sql);
</pre>
<p>So eventually when you want to dump this &#8220;complex&#8221; query, or whatever query there is, you can simply &#8220;echo&#8221; it and see what’s its syntax. </p>
<pre lang="PHP">
// this query is WRONG because of the where clause
$sql = "SELECT * FROM my_table WHERE id = ";

// dump and debug the wrong query
die($sql);

// this line won't be executed
$resource = mysql_query($sql);
</pre>
<p>So far so good, but things appear to be a bit different when you start to work with <a href="http://framework.zend.com/" title="Zend Framework" target="_blank">Zend Framework</a>. Higher levels of abstraction come with slightly more difficult ways to dump (debug) your SQL queries.</p>
<p>OK you&#8217;ve two options. Using <a href="http://framework.zend.com/manual/en/zend.db.select.html" title="Zend_Db_Select" target="_blank">Zend_Db_Select</a> or &#8230; not.<br />
<span id="more-2709"></span></p>
<h2>Dump Zend_Db_Select Query</h2>
<p>Debugging queries generated with Zend_Db_Select is as easy as the following snippet. Let’s say there’s a db table “users” and typically the model is called Users.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>Obviously this code is wrong, because the &#8220;where&#8221; clause is wrong and we don&#8217;t pass any values to it. So now the question is how can we dump this SQL. Well, since we use Zend_Db_Select, so the simplest way to dump the SQL is by calling __toString(). The code above will become as follows.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function getSomeUsers()
	{
		$select = $this->select()
				->from(this->_name)
				->where('id > ');

		die($select->__toString());

		$rows = $this->fetchAll($select);
	}
}
</pre>
<p>That&#8217;s great, but how can we dump update/insert queries for instance?</p>
<h2>Dump with Zend_Db_Profiler</h2>
<p>The way you should dump insert or update queries is a bit different. You should use <a href="http://framework.zend.com/manual/en/zend.db.profiler.html" title="Zend_Db_Profiler" target="_blank">Zend_Db_Profiler</a>, which is a more complex way to debug (profile) your queries. You can not only debug insert or update queries, of course, but any queries generated by the Zend_Db abstraction. Let’s see how.</p>
<p>Now from a controller perspective this model can be just called as:</p>
<pre lang="PHP">
$users = new Users();
$users->update(array('name' => 'my name'), 'id =');
</pre>
<p>Obviously this code is wrong, again because of the where clause and now the question is how we can debug the generated SQL. The answer is: using Zend_Db_Profiler. Looking back again to the model, this should look like this.</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = ‘users’;

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
	}
}
</pre>
<p>The thing is that since Zend_Db is using prepared statements, the last row (the one with the Zend_Debug::&#8230;) will dump something like the following line.</p>
<pre lang="SQL">
UPDATE `users` SET `name` = ? WHERE (id =)
</pre>
<p>So as you can see, actually here you don’t see the name’s value, which should be “my name”. To see this you should call getQueryParams().</p>
<pre lang="PHP">
class Users extends Zend_Db_Table
{
	protected $_name = 'users';

	public function updateSomeUsers()
	{
		// first enable the profiler
		$this->getProfiler()->setEnabled(true);

		// try to execute
		$this->update(array('name' => 'my name'), 'id = ');

		// dump the SQL
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQuery());
		Zend_Debug::dump($this->getProfiler()->getLastQueryProfile()->getQueryParams());

		// don't forget to disable the profiler is
		// you don't need it anymore
		$this->getProfiler()->setEnabled(false);
</pre>
<p>Now the debug info will be something like this.</p>
<pre lang="PHP">
UPDATE `users` SET `name` = ? WHERE (id =)
array
	1 => string 'my name' (length 7)
</pre>
<p>Calling this from the controller there are some changes. Take a look at the example bellow.</p>
<pre lang="PHP">
$users = new Users();

// enable the profiler
$users->getAdapter()->getProfiler()->setEnabled(true);

// execute (or at least try to)
$users->update(array('name' => 'my name'), 'id =');

// debugdump the SQL
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQuery());
Zend_Debug::dump($users->getAdapter()->getProfiler()->getLastQueryProfile()->getQueryParams());

// don't forget to disable the profiler is
// you don't need it anymore
$users->getAdapter()->getProfiler()->setEnabled(false);
</pre>
<p>In this case you should use the longer model_name->getAdapter()</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/04/29/debugging-in-zend-framework/" rel="bookmark" title="Debugging in Zend Framework">Debugging in Zend Framework </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/07/28/which-model-should-contain-that-method/" rel="bookmark" title="Which Model Should Contain That Method?">Which Model Should Contain That Method? </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fetching Rows With Zend_Db fetch()</title>
		<link>/2010/08/05/fetching-rows-with-zend_db-fetch/</link>
		<comments>/2010/08/05/fetching-rows-with-zend_db-fetch/#comments</comments>
		<pubDate>Thu, 05 Aug 2010 14:14:51 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data management]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SQL keywords]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">/?p=1881</guid>
		<description><![CDATA[Fetching the Entire Row Set 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 &#8230; <a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" class="more-link">Continue reading <span class="screen-reader-text">Fetching Rows With Zend_Db fetch()</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
<li><a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" rel="bookmark" title="How to Dump the Generated Zend_Db SQL Query">How to Dump the Generated Zend_Db SQL Query </a></li>
<li><a href="/2010/04/21/setting-a-zend-framework-_redirect-referer/" rel="bookmark" title="Setting a Zend Framework _redirect Referer">Setting a Zend Framework _redirect Referer </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Fetching the Entire Row Set</h2>
<p><a href="/wp-content/uploads/2010/08/rows.jpg"><img class="aligncenter size-full wp-image-1891" title="rows" src="/wp-content/uploads/2010/08/rows.jpg" alt="Rows" width="430" height="262" srcset="/wp-content/uploads/2010/08/rows.jpg 430w, /wp-content/uploads/2010/08/rows-300x182.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>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:</p>
<pre lang="php">
<?php
class User extends Zend_Db_Table
{
		
	public function listAll()
	{
		$query = "SELECT * FROM user";
	
		// exec query
		$rs = $this->getAdapter()->query($query);
	
		return $rs->fetchAll();
		
	}
	
}
</pre>
<p>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?</p>
<h2>Fetching a Row</h2>
<p>By simply change the code like so:</p>
<pre lang="php">
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;
	}
}
</pre>
<p>you can modify the rows and you&#8217;ll have the same result.</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2011/08/17/php-fetch-get-as-string-with-http_build_query/" rel="bookmark" title="PHP: Fetch $_GET as String with http_build_query()">PHP: Fetch $_GET as String with http_build_query() </a></li>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
<li><a href="/2012/02/09/how-to-dump-the-generated-zend_db-sql-query/" rel="bookmark" title="How to Dump the Generated Zend_Db SQL Query">How to Dump the Generated Zend_Db SQL Query </a></li>
<li><a href="/2010/04/21/setting-a-zend-framework-_redirect-referer/" rel="bookmark" title="Setting a Zend Framework _redirect Referer">Setting a Zend Framework _redirect Referer </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/08/05/fetching-rows-with-zend_db-fetch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Which Model Should Contain That Method?</title>
		<link>/2010/07/28/which-model-should-contain-that-method/</link>
		<comments>/2010/07/28/which-model-should-contain-that-method/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 15:07:41 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Business/Finance]]></category>
		<category><![CDATA[Computer science]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data management]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database management systems]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[From]]></category>
		<category><![CDATA[Human Interest]]></category>
		<category><![CDATA[IBM software]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL keywords]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[zend]]></category>

		<guid isPermaLink="false">/?p=1866</guid>
		<description><![CDATA[Let&#8217;s say you have two models &#8211; each one modeling a database table &#8211; Users &#38; Article. Here there&#8217;s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application. What happens if you have to write a getUserArticles method? Where would you put it? Whether this &#8230; <a href="/2010/07/28/which-model-should-contain-that-method/" class="more-link">Continue reading <span class="screen-reader-text">Which Model Should Contain That Method?</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s say you have two models &#8211; each one modeling a database table &#8211; <strong>Users</strong> &amp; <strong>Article</strong>. Here there&#8217;s nothing to deal with Zend Framwork, but you can think of them as typical models in a ZF application.</p>
<p>What happens if you have to write a getUserArticles method? Where would you put it? Whether this will be the User model or the Article model?<a href="/wp-content/uploads/2010/07/question.jpg"><img class="aligncenter size-full  wp-image-1875" title="question" src="/wp-content/uploads/2010/07/question.jpg" alt="Where?" width="430" height="231" srcset="/wp-content/uploads/2010/07/question.jpg 430w, /wp-content/uploads/2010/07/question-300x161.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Although technically you can put it in both models my advice is to look at the SQL query. If the FROM clause is containing the user table &#8211; than put the method in the User model, but here you&#8217;d have something like:</p>
<pre lang="sql">
SELECT * FROM Article WHERE user_id = 1
</pre>
<p>I&#8217;d prefer to place it in the Article model!</p>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/07/12/mvc-in-practice-designing-models/" rel="bookmark" title="MVC in Practice: Designing Models">MVC in Practice: Designing Models </a></li>
<li><a href="/2010/07/19/zend-framework-cache-database-table-schemes/" rel="bookmark" title="Zend Framework: Cache Database Table Schemes">Zend Framework: Cache Database Table Schemes </a></li>
<li><a href="/2010/08/05/fetching-rows-with-zend_db-fetch/" rel="bookmark" title="Fetching Rows With Zend_Db fetch()">Fetching Rows With Zend_Db fetch() </a></li>
<li><a href="/2010/04/26/mysql-expressions-in-zend-framework/" rel="bookmark" title="MySQL Expressions in Zend Framework">MySQL Expressions in Zend Framework </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/28/which-model-should-contain-that-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
