<?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>NewSourceMedia Blog &#187; update data</title>
	<atom:link href="http://newsourcemedia.com/blog/tag/update-data/feed/" rel="self" type="application/rss+xml" />
	<link>http://newsourcemedia.com/blog</link>
	<description>Focused on Interactive Design, Development and Marketing</description>
	<lastBuildDate>Wed, 11 Aug 2010 13:56:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP Search Between Values in MySQL Database</title>
		<link>http://newsourcemedia.com/blog/php-search-between-values-in-mysql-database/</link>
		<comments>http://newsourcemedia.com/blog/php-search-between-values-in-mysql-database/#comments</comments>
		<pubDate>Sat, 17 Jul 2004 00:22:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL / Database]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[delete data]]></category>
		<category><![CDATA[from MySQL]]></category>
		<category><![CDATA[manipulate data]]></category>
		<category><![CDATA[select data]]></category>
		<category><![CDATA[update data]]></category>

		<guid isPermaLink="false">http://newsourcemedia.com/blog/2004/07/16/php-search-between-values-in-mysql-database/</guid>
		<description><![CDATA[Need to know how to filter a database search result between two values. Use the WHERE clause in SQL to provides conditions in your search.

A Tutorial on how to search or select between values from a Database using MySQL and or PHP.
This would most likely be use for filtering a search for things like a [...]]]></description>
			<content:encoded><![CDATA[<p>Need to know how to filter a database search result between two values. Use the WHERE clause in SQL to provides conditions in your search.<br />
<span id="more-191"></span><br />
<strong>A Tutorial on how to search or select between values from a Database using MySQL and or PHP.</strong></p>
<p>This would most likely be use for filtering a search for things like a time/date  span, price range, weight range &#8230;etc.</p>
<p><span class="sourceCODE">SELECT * FROM table WHERE field BETWEEN $low AND $high</span></p>
<p>or</p>
<p><span class="sourceCODE">SELECT * FROM table WHERE field &gt;= $num1 AND field  &lt;= $num2</span></p>
<p>The codes below I found very useful.</p>
<h3>the <code>WHERE</code> clause.</h3>
<p>The <code>WHERE</code> clause in SQL provides conditions that must be met in order for a particular row within a table to be part of the result set. To use this clause, simply append it to the end of any applicable query as shown:</p>
<p><!-- sidebar begins --> <!-- don't move sidebars --> <!-- sidebar ends --></p>
<pre class="code"><code>mysql&gt; SELECT * FROM books WHERE author_id=1;

 --------- ----------- ------------------ ------------
| book_id | author_id | title            | pub_date   |
 --------- ----------- ------------------ ------------
| 1       | 1         | PHP Unleashed    | 2003-11-01 |
| 2       | 1         | PHP4 Programming | 2002-10-01 |
 --------- ----------- ------------------ ------------ 

2 rows in set (0.00 sec)</code></pre>
<p>The SQL <code>WHERE</code> clause supports several different logical operations. Here are the most common, represented in terms of <code>A</code> and <code>B</code>:</p>
<table border="1" cellspacing="0" cellpadding="3">
<tbody>
<tr>
<th>Expression</th>
<th>Evaluation</th>
</tr>
<tr>
<td>A = B</td>
<td>True if A equals B</td>
</tr>
<tr>
<td>A != B</td>
<td>True if A does not equal B</td>
</tr>
<tr>
<td>A &lt;= B</td>
<td>True if A is less than or equal to B</td>
</tr>
<tr>
<td>A &gt; B</td>
<td>True if A is greater than or equal to B</td>
</tr>
<tr>
<td>A &lt; B</td>
<td>True if A is less than B</td>
</tr>
<tr>
<td>A &gt; B</td>
<td>True if A is greater than B</td>
</tr>
<tr>
<td>A &lt;=&gt; B</td>
<td>True if A is equal to B (NULL Safe)</td>
</tr>
<tr>
<td>A IS NULL</td>
<td>True if A is NULL</td>
</tr>
<tr>
<td>A IS NOT NULL</td>
<td>True if A is not NULL</td>
</tr>
<tr>
<td>A BETWEEN M AND N</td>
<td>True if A is between values M and N</td>
</tr>
<tr>
<td>A NOT BETWEEN M AND N</td>
<td>True if A is not between values M and N</td>
</tr>
<tr>
<td>A IN(value, value2, &#8230;)</td>
<td>True if A is one of the listed values</td>
</tr>
<tr>
<td>A NOT IN (value, value2, &#8230;)</td>
<td>True if A is not one of the listed values</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong> The reason for a special <code>NULL</code> Safe comparison operator (the <code>&lt;=&gt;</code> operator) relates to the way MySQL handles <code>NULL</code> values.  By definition, the comparison of two <code>NULL</code> values is false (since <code>NULL</code> is an undefined value). You must use this operator when comparing two values in SQL that may be <code>NULL</code>.</p>
<p>The <code>LIKE</code> operator matches parts of strings. For example, to return a result set containing all of the books that have <code>PHP</code> in their title:</p>
<pre class="code"><code>mysql&gt; SELECT * FROM books WHERE title LIKE "%PHP%";

 --------- ----------- ------------------ ------------
| book_id | author_id | title            | pub_date   |
 --------- ----------- ------------------ ------------
| 1       | 1         | PHP Unleashed    | 2003-11-01 |
| 2       | 1         | PHP4 Programming | 2002-10-01 |
 --------- ----------- ------------------ ------------ 

2 rows in set (0.04 sec)</code></pre>
<p>The syntax for the <code>LIKE</code> operator is <code>LIKE &lt;pattern&gt;</code> where the percent sign (<code>%</code>) serves as a wildcard. In the above example, the pattern <code>%PHP%</code> matches every row whose <code>title</code> column has the string <code>PHP</code>.  This is case-insensitive.</p>
<p><strong>Note:</strong> If you would like to match the literal percent character, escape it by doubling it; i.e., <code>%%</code>.</p>
<p>Although I have introduced the <code>WHERE</code> clause to you in terms of the <code>SELECT</code> statement, this technique of identifying particular rows within a table works when deleting and updating records as well. To update specific records within a table, use an <code>UPDATE</code> statement instead of a <code>SELECT</code> statement. For instance, assume I no longer live in Michigan, having moved to warmer climes. The first step in modifying the records is to identify which record in the <code>authors</code> table needs to change:</p>
<pre class="code"><code>mysql&gt; SELECT * FROM authors WHERE name LIKE "%Coggeshall";

 ----------- ----------------- -------
| author_id | name            | state |
 ----------- ----------------- -------
| 1         | John Coggeshall | MI    |
 ----------- ----------------- ------- 

1 row in set (0.00 sec)</code></pre>
<p>As you can see, my <code>author_id</code> is <code>1</code>. Since this number is assumed in our table to be a unique identifier (called a primary key) for my record, we can use it in conjunction with the <code>UPDATE</code> statement to modify my specific record in the table:</p>
<pre class="code"><code>mysql&gt; UPDATE authors SET state='FL' WHERE author_id=1;

Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0</code></pre>
<p><strong>Note:</strong> Although we technically could have used the same <code>WHERE</code> clause as the <code>SELECT</code> statement used to identify the <code>author_id</code> value, it is considered general bad practice to rely on such conditionals as the basis of which rows within a table are modified. For instance, what would have happened had there been two people with the last name <code>Coggeshall</code>? Always use a unique identifier as the conditional when performing modifications.</p>
<p>As you can see, the above query modifies a single row (since there is only one row with an <code>author_id</code> value of <code>1</code>), changing the <code>state</code> column to <code>FL</code>. The general syntax of the <code>UPDATE</code> statement is:</p>
<pre class="code"><code>UPDATE &lt;table&gt; SET &lt;column&gt;=&lt;value&gt;, &lt;column&gt;=&lt;value&gt;, ... WHERE &lt;conditions&gt;</code></pre>
<p><code>&lt;table&gt;</code> represents the table name being manipulated, and the column/value pairs represent the individual values to modify in the rows identified by the <code>WHERE</code> clause. Although it is rare, there are times where you would like to universally modify the value of a particular column in a table. In these cases, you may omit the <code>WHERE</code> clause entirely.  Be very cautious, though.</p>
<p>If it&#8217;s possible to modify existing records within a table, obviously it must also be possible to remove certain records from tables. This operation is handled through the appropriately named <code>DELETE</code> statement. For instance, to remove my record from the <code>authors</code> table, use the following statement:</p>
<pre class="code"><code>mysql&gt; DELETE FROM authors WHERE author_id=1;

Query OK, 1 row affected (0.06 sec)</code></pre>
<p><strong>Note:</strong> To follow along with the examples later in the column, the row deleted in this example must actually exist. The above is shown only as an example.</p>
<p>As you can see, deleting rows from a table is a fairly straightforward process. The general syntax for the <code>DELETE</code> statement is as follows:</p>
<pre class="code"><code>DELETE FROM &lt;table&gt; WHERE &lt;conditions&gt;</code></pre>
<p>As was the case with the <code>UPDATE</code> statement, you can omit the <code>WHERE</code> clause in the <code>DELETE</code> statement, causing the entire contents of the table to be deleted. In SQL, there is no (reasonable) way to recover data once it has been deleted from a table, so take extreme caution to ensure that any <code>DELETE</code> queries will behave as expected. It&#8217;s often useful to issue a <code>SELECT COUNT(*)</code> query with the <code>WHERE</code> clause to verify that you&#8217;re affecting only the rows you want to delete.</p>
<p>Also note that unlike the example provided, when removing the entire contents of a table, MySQL will not report how many rows were affected. This is due to the fact that a table could potentially contain thousands of rows and to count each individual row when they all are being removed is simply inefficient.</p>
<p>The final two statements of the day are used to modify tables themselves within a database. The <code>ALTER</code> statement changes the properties of tables themselves (for instance, adding an entirely new column to the table definition). If you wanted to add a column that kept track of the ISBN for each book in the <code>books</code> table, you could add that column to the table in the following fashion:</p>
<pre class="code"><code>mysql&gt; ALTER TABLE books ADD isbn VARCHAR(25) AFTER pub_date;

Query OK, 4 rows affected (0.10 sec)

Records: 4 Duplicates: 0 Warnings: 0</code></pre>
<p>In this case, I have added a new column named <code>isbn</code> of type <code>VARCHAR</code> as the last item in the table by placing it after the <code>pub_date</code> column (itself previously the last column in the <code>books</code> table). To visualize this, use the <code>DESC</code> statement:</p>
<pre class="code"><code>mysql&gt; DESC books;

 ----------- -------------- ------------------- ------ ----- --------- ----------------
| Field     | Type         | Collation         | Null | Key | Default | Extra          |
 ----------- -------------- ------------------- ------ ----- --------- ----------------
| book_id   | int(11)      | binary            | YES  | PRI | NULL    | auto_increment |
| author_id | int(11)      | binary            | YES  |     | NULL    |                |
| title     | varchar(255) | latin1_swedish_ci | YES  |     | NULL    |                |
| pub_date  | date         | latin1_swedish_ci | YES  |     | NULL    |                |
| isbn      | varchar(25)  | latin1_swedish_ci | YES  |     | NULL    |                |
 ----------- -------------- ------------------- ------ ----- --------- ---------------- 

5 rows in set (0.00 sec)</code></pre>
<p>To remove this column from the table, use the <code>ALTER</code> statement again.  However, instead of using the <code>ADD</code> qualifier, use <code>DROP</code>:</p>
<pre class="code"><code>mysql&gt; ALTER TABLE books DROP isbn;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0</code></pre>
<p>To add a new column to a table as the first column, use the <code>FIRST</code> qualifier used instead of <code>AFTER</code>:</p>
<pre class="code"><code>mysql&gt; ALTER TABLE books ADD isbn VARCHAR(25) FIRST;
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0</code></pre>
<p>Here&#8217;s the general syntax of the <code>ALTER</code> statement:</p>
<pre class="code"><code>ALTER TABLE &lt;table&gt; [ADD | DROP] &lt;column definition&gt; [FIRST | AFTER &lt;column&gt;]</code></pre>
<p><strong>Note:</strong> When altering a table that already contains data, the default value for the additional column will be used to populate those records which already exist.</p>
<p>The final statement I will introduce is the <code>DROP</code> statement. This statement can delete entire databases as well as individual tables within a database. Since the syntax for this statement is incredibly simple, I will just give you the general syntax:</p>
<pre class="code"><code>DROP [TABLE | DATABASE] &lt;table or database name&gt;</code></pre>
<p>As was the case with the <code>DELETE</code> statement, it is extremely important to be cautious when deleting any table or database! Although technically feasible, the process of recovering deleted data from a database can be extremely time-consuming (not to mention extremely costly).</p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://newsourcemedia.com/blog/create-mysql-database-with-php/" rel="bookmark">Create MySQL Database With PHP</a></li><li><a href="http://newsourcemedia.com/blog/php-mysql-order-by-two-columns/" rel="bookmark">PHP MySQL Order by Two Columns</a></li><li><a href="http://newsourcemedia.com/blog/mysql-table-fields-data-types/" rel="bookmark">MySQL Table Fields Data Types</a></li><li><a href="http://newsourcemedia.com/blog/query-select-from-a-database/" rel="bookmark">Query Select From a Database</a></li><li><a href="http://newsourcemedia.com/blog/php-how-to-find-odd-and-even-numbers/" rel="bookmark">PHP how to find Odd and Even numbers</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://newsourcemedia.com/blog/php-search-between-values-in-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->