<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Building a PHP and MySQL search function</title>
	<atom:link href="http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/</link>
	<description>Woodsman, adventurer, tech enthusiast, and lucky man.</description>
	<lastBuildDate>Mon, 15 Mar 2010 02:20:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: J.D.</title>
		<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/comment-page-1/#comment-570</link>
		<dc:creator>J.D.</dc:creator>
		<pubDate>Sat, 10 Oct 2009 02:31:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.jdhodges.com/?p=327#comment-570</guid>
		<description>Thanks for pointing that out John. I clean the input string before I send it to the function.

However, it would be wise to include some rudimentary cleaning [string escaping] in the function itself (just in case it ever got called with raw input). So I&#039;m going to update the code in this post. :-)</description>
		<content:encoded><![CDATA[<p>Thanks for pointing that out John. I clean the input string before I send it to the function.</p>
<p>However, it would be wise to include some rudimentary cleaning [string escaping] in the function itself (just in case it ever got called with raw input). So I&#8217;m going to update the code in this post. <img src='http://www.jdhodges.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Havlik</title>
		<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/comment-page-1/#comment-569</link>
		<dc:creator>John Havlik</dc:creator>
		<pubDate>Fri, 09 Oct 2009 22:33:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.jdhodges.com/?p=327#comment-569</guid>
		<description>Just a note on that code, it doesn&#039;t appear to use any input string escaping, which means grabbing from any of the PHP superglobals, and any other untrusted source, will need to be escaped before running through the function. Otherwise you open yourself up to nasty SQL injection attacks.</description>
		<content:encoded><![CDATA[<p>Just a note on that code, it doesn&#8217;t appear to use any input string escaping, which means grabbing from any of the PHP superglobals, and any other untrusted source, will need to be escaped before running through the function. Otherwise you open yourself up to nasty SQL injection attacks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: J.D.</title>
		<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/comment-page-1/#comment-568</link>
		<dc:creator>J.D.</dc:creator>
		<pubDate>Fri, 09 Oct 2009 13:57:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.jdhodges.com/?p=327#comment-568</guid>
		<description>Yo guys. I did have to make some changes to get the code to work. I wanted it to generate the multiword SQL querystring for me, so I modified it to be a function. The function now simply returns a SQL querystring, which you can then use however you like. Here&#039;s a rather ugly version of the code, hope it helps:
&lt;p&gt;&#160;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;// call the function, it returns a query string&lt;br&gt;
  $queryString = multiquery(&quot;some search terms&quot;, &quot;table_to_select_from&quot;);&lt;br&gt;
  //print the querystring, use this querystring to get whatever recordset you want&lt;br&gt;
echo $queryString;&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;br&gt;
  // Original code: http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output&lt;br&gt;
  &lt;strong&gt;function multiquery($multiquery, $vartable) {&lt;/strong&gt;&lt;br&gt;
  // escape the input string, I know mysql_escape_string is deprecated, but I don&#039;t want to mess with another function tonight...&lt;br&gt;
  $multiquery = mysql_escape_string($multiquery);&lt;br&gt;
  // checks if a search has been submitted&lt;br&gt;
  if(!empty($multiquery))&lt;br&gt;
  {&lt;br&gt;
  // the table to search&lt;br&gt;
  $table = $vartable;&lt;br&gt;
  // explode search words into an array&lt;br&gt;
  $arraySearch = explode(&quot; &quot;, $multiquery);&lt;br&gt;
  // table fields to search&lt;br&gt;
  $arrayFields = array(0 =&gt; &quot;Long_Desc&quot;, 1 =&gt; &quot;Shrt_Desc&quot;, 2 =&gt; &quot;ComName&quot;, 3 =&gt; &quot;ManufacName&quot;);&lt;br&gt;
  $countSearch = count($arraySearch);&lt;br&gt;
  $a = 0;&lt;br&gt;
  $b = 0;&lt;br&gt;
  $query = &quot;SELECT * FROM &quot;.$table.&quot; WHERE (&quot;;&lt;br&gt;
  $countFields = count($arrayFields);&lt;br&gt;
  while ($a &lt; $countFields)&lt;br&gt;
  {&lt;br&gt;
  while ($b &lt; $countSearch)&lt;br&gt;
  {&lt;br&gt;
  $query = $query.&quot;$arrayFields[$a] LIKE &#039;%$arraySearch[$b]%&#039;&quot;;&lt;br&gt;
  $b++;&lt;br&gt;
  if ($b &lt; $countSearch)&lt;br&gt;
  {&lt;br&gt;
  $query = $query.&quot; AND &quot;;&lt;br&gt;
  }&lt;br&gt;
  }&lt;br&gt;
  $b = 0;&lt;br&gt;
  $a++;&lt;br&gt;
  if ($a &lt; $countFields)&lt;br&gt;
  {&lt;br&gt;
  $query = $query.&quot;) OR (&quot;;&lt;br&gt;
  }&lt;br&gt;
  }&lt;br&gt;
  &lt;strong&gt;// just make a querystring &quot;$query&quot;, don&#039;t actually get the query_result here&lt;br&gt;
  &lt;/strong&gt;$query = $query.&quot;)&quot;;&lt;br&gt;
  //$query_result = mysql_query($query);&lt;br&gt;
  &lt;strong&gt;//return the querystring for use outside the function&lt;/strong&gt;&lt;br&gt;
  return $query;&lt;br&gt;
  &lt;strong&gt;//disable this portion, since we&#039;re not working with a query result anymore (just a querystring to be used later)&lt;br&gt;
  &lt;/strong&gt;if(1 &lt; 1)&lt;br&gt;
  {&lt;br&gt;
  echo &#039;&lt;p&gt;No matches found for &quot;&#039;.$search.&#039;&quot;&lt;/p&gt;&#039;;&lt;br&gt;
  }&lt;br&gt;
  else&lt;br&gt;
  {&lt;br&gt;
  echo &#039;&lt;p&gt;Search Results for &quot;&#039;.$search.&#039;&quot;:&lt;/p&gt;&#039;.&quot;\n\n&quot;;&lt;br&gt;
  // output list of articles&lt;br&gt;
  while($row = mysql_fetch_assoc($query_result))&lt;br&gt;
  {&lt;br&gt;
  // output whatever you want here for each search result&lt;br&gt;
  echo &#039;&lt;a href=&quot;index.php?id=&#039;.$row[&#039;id&#039;].&#039;&quot;&gt;&#039;.$row[&#039;title&#039;].&#039;&lt;/a&gt;&lt;br /&gt;&#039;;&lt;br&gt;
  }&lt;br&gt;
  }&lt;br&gt;
  }&lt;br&gt;
  else&lt;br&gt;
  {&lt;br&gt;
  // display a welcome page&lt;br&gt;
  }&lt;br&gt;
  &lt;strong&gt;}&lt;br&gt;
// end search function&lt;/strong&gt;&lt;/code&gt;&lt;/p&gt;

THE END.</description>
		<content:encoded><![CDATA[<p>Yo guys. I did have to make some changes to get the code to work. I wanted it to generate the multiword SQL querystring for me, so I modified it to be a function. The function now simply returns a SQL querystring, which you can then use however you like. Here&#8217;s a rather ugly version of the code, hope it helps:</p>
<p>&nbsp;</p>
<p><code>// call the function, it returns a query string<br />
  $queryString = multiquery(&quot;some search terms&quot;, &quot;table_to_select_from&quot;);<br />
  //print the querystring, use this querystring to get whatever recordset you want<br />
echo $queryString;<br />
</code><code><br />
  // Original code: <a href="http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output" rel="nofollow">http://www.acuras.co.uk/articles/2-php-multi-word-mysql-search-algorithm-and-output</a><br />
  <strong>function multiquery($multiquery, $vartable) {</strong><br />
  // escape the input string, I know mysql_escape_string is deprecated, but I don't want to mess with another function tonight...<br />
  $multiquery = mysql_escape_string($multiquery);<br />
  // checks if a search has been submitted<br />
  if(!empty($multiquery))<br />
  {<br />
  // the table to search<br />
  $table = $vartable;<br />
  // explode search words into an array<br />
  $arraySearch = explode(&quot; &quot;, $multiquery);<br />
  // table fields to search<br />
  $arrayFields = array(0 =&gt; &quot;Long_Desc&quot;, 1 =&gt; &quot;Shrt_Desc&quot;, 2 =&gt; &quot;ComName&quot;, 3 =&gt; &quot;ManufacName&quot;);<br />
  $countSearch = count($arraySearch);<br />
  $a = 0;<br />
  $b = 0;<br />
  $query = &quot;SELECT * FROM &quot;.$table.&quot; WHERE (&quot;;<br />
  $countFields = count($arrayFields);<br />
  while ($a &lt; $countFields)<br />
  {<br />
  while ($b &lt; $countSearch)<br />
  {<br />
  $query = $query.&quot;$arrayFields[$a] LIKE '%$arraySearch[$b]%'&quot;;<br />
  $b++;<br />
  if ($b &lt; $countSearch)<br />
  {<br />
  $query = $query.&quot; AND &quot;;<br />
  }<br />
  }<br />
  $b = 0;<br />
  $a++;<br />
  if ($a &lt; $countFields)<br />
  {<br />
  $query = $query.&quot;) OR (&quot;;<br />
  }<br />
  }<br />
  <strong>// just make a querystring &quot;$query&quot;, don't actually get the query_result here<br />
  </strong>$query = $query.&quot;)&quot;;<br />
  //$query_result = mysql_query($query);<br />
  <strong>//return the querystring for use outside the function</strong><br />
  return $query;<br />
  <strong>//disable this portion, since we're not working with a query result anymore (just a querystring to be used later)<br />
  </strong>if(1 &lt; 1)<br />
  {<br />
  echo '&lt;p&gt;No matches found for &quot;'.$search.'&quot;&lt;/p&gt;';<br />
  }<br />
  else<br />
  {<br />
  echo '&lt;p&gt;Search Results for &quot;'.$search.'&quot;:&lt;/p&gt;'.&quot;\n\n&quot;;<br />
  // output list of articles<br />
  while($row = mysql_fetch_assoc($query_result))<br />
  {<br />
  // output whatever you want here for each search result<br />
  echo '&lt;a href=&quot;index.php?id='.$row['id'].'&quot;&gt;'.$row['title'].'&lt;/a&gt;&lt;br /&gt;';<br />
  }<br />
  }<br />
  }<br />
  else<br />
  {<br />
  // display a welcome page<br />
  }<br />
  <strong>}<br />
// end search function</strong></code></p>
<p>THE END.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Havlik</title>
		<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/comment-page-1/#comment-566</link>
		<dc:creator>John Havlik</dc:creator>
		<pubDate>Thu, 08 Oct 2009 20:27:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.jdhodges.com/?p=327#comment-566</guid>
		<description>Catherine,

If JD is using it on komparison then it should work with PHP5, our servers are running PHP 5.2.</description>
		<content:encoded><![CDATA[<p>Catherine,</p>
<p>If JD is using it on komparison then it should work with PHP5, our servers are running PHP 5.2.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Catherine</title>
		<link>http://www.jdhodges.com/2009/09/building-a-php-and-mysql-search-function/comment-page-1/#comment-565</link>
		<dc:creator>Catherine</dc:creator>
		<pubDate>Thu, 08 Oct 2009 12:20:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.jdhodges.com/?p=327#comment-565</guid>
		<description>I was very glad to find the link to: &quot;PHP: Multi-word MySQL Search Algorithm and Output&quot;, but after 6 hours of trying to make it work, I&#039;m stumped!  It seems that it works on older versions of php, but not on my 5.1 version.  Can you help me in any way?</description>
		<content:encoded><![CDATA[<p>I was very glad to find the link to: &#8220;PHP: Multi-word MySQL Search Algorithm and Output&#8221;, but after 6 hours of trying to make it work, I&#8217;m stumped!  It seems that it works on older versions of php, but not on my 5.1 version.  Can you help me in any way?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
