<?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>select &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/select/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>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>jQuery: Get the Selected Option Text</title>
		<link>/2010/06/22/jquery-get-the-selected-option-text/</link>
		<comments>/2010/06/22/jquery-get-the-selected-option-text/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 17:33:56 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[Markup languages]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[Technical communication]]></category>

		<guid isPermaLink="false">/?p=1660</guid>
		<description><![CDATA[What&#8217;s the Task? First of all let me explain what is the task. If there&#8217;s a select menu with several options, each of which are with a given value attribute, the task is to get the text into the option tag, and not that of the value attribute. In that scenario let&#8217;s assume that we &#8230; <a href="/2010/06/22/jquery-get-the-selected-option-text/" class="more-link">Continue reading <span class="screen-reader-text">jQuery: Get the Selected Option Text</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/09/27/jquery-setting-up-a-vector-path-fill-color/" rel="bookmark" title="jQuery: Setting Up a Vector Path Fill Color">jQuery: Setting Up a Vector Path Fill Color </a></li>
<li><a href="/2011/04/05/jquery-unbind/" rel="bookmark" title="jQuery.unbind()">jQuery.unbind() </a></li>
<li><a href="/2010/02/16/jquery-tooltip-plugin/" rel="bookmark" title="jQuery tooltip plugin!">jQuery tooltip plugin! </a></li>
<li><a href="/2010/04/06/jquery-what-about-this-title/" rel="bookmark" title="jQuery: what about this.title?">jQuery: what about this.title? </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>What&#8217;s the Task?</h2>
<p>First of all let me explain what is the task. If there&#8217;s a select menu with several options, each of which are with a given value attribute, the task is to get the text into the option tag, and not that of the value attribute.</p>
<p>In that scenario let&#8217;s assume that we have the following HTML:</p>
<pre lang="html4strict">
<select id="my-select">
    <option value="1">value 1</option>
    <option value="2">value 2</option>
</select>
</pre>
<p><a href="/wp-content/uploads/2010/06/Picture-1.png"><img src="/wp-content/uploads/2010/06/Picture-1.png" alt="select menu" title="Select Menu" width="181" height="95" class="alignleft size-full wp-image-1671" /></a><br />
Note that in the official jQuery doc page, the selector is described in the same context, but with a different markup:</p>
<pre lang="html4strict">
<select id="my-select">
    <option>value 1</option>
    <option>value 2</option>
</select>
</pre>
<p>than the jQuery snippet looks like that:</p>
<pre lang="javascript">
$(document).ready(function() {
    $("#my-select").change(function() {
        alert($(this).val());
    });
});
</pre>
<p>Here&#8217;s interesting to note that there&#8217;s no value attribute set to the options, thus the selection of an element is correct and the returned value is correct. The problem is that this code doesn&#8217;t work correctly once you&#8217;ve a different value attribute, from the value enclosed into the option tag. In our case we&#8217;ve the markup shown on the first code snippet and we&#8217;d like to get the &#8220;value 2&#8221; string instead of &#8220;2&#8221;.<br />
<a href="/wp-content/uploads/2010/06/Picture-2.png"><img src="/wp-content/uploads/2010/06/Picture-2.png" alt="" title="Alert screen" width="329" height="130" class="alignleft size-full wp-image-1672" srcset="/wp-content/uploads/2010/06/Picture-2.png 329w, /wp-content/uploads/2010/06/Picture-2-300x118.png 300w" sizes="(max-width: 329px) 100vw, 329px" /></a></p>
<h2>Source Code</h2>
<pre lang="html4strict">
<select id="my-select">
    <option value="1">value 1</option>
    <option value="2">value 2</option>
</select>
</pre>
<pre lang="javascript">
$(document).ready(function() {
    $("#my-select").change(function() {
        alert($(this).val());
    });
});
</pre>
<h2>Solution</h2>
<p>Finally the solution, is pretty simple and clear, but this time is not to so &#8220;native&#8221; and it&#8217;s definitely something you couldn&#8217;t expect! The only thing you&#8217;ve to do is to change the selector!</p>
<pre lang="javascript">
$(document).ready(function() {
    $("#my-select).change(function() {
        alert($('#my-select option:selected').html());
    });
});
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/09/27/jquery-setting-up-a-vector-path-fill-color/" rel="bookmark" title="jQuery: Setting Up a Vector Path Fill Color">jQuery: Setting Up a Vector Path Fill Color </a></li>
<li><a href="/2011/04/05/jquery-unbind/" rel="bookmark" title="jQuery.unbind()">jQuery.unbind() </a></li>
<li><a href="/2010/02/16/jquery-tooltip-plugin/" rel="bookmark" title="jQuery tooltip plugin!">jQuery tooltip plugin! </a></li>
<li><a href="/2010/04/06/jquery-what-about-this-title/" rel="bookmark" title="jQuery: what about this.title?">jQuery: what about this.title? </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/06/22/jquery-get-the-selected-option-text/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
