Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyword Searching

Status
Not open for further replies.

Phutie

Programmer
Jul 31, 2000
29
0
0
PH
Hi,<br><br>&nbsp;I just want to print the content of the query. I hope you can check out<br>&nbsp;if my code is correct:<br><br>&nbsp;create a database with a table named KeyWords.<br><br>&nbsp;create table KeyWords (<br>&nbsp;KwID int(7),<br>&nbsp;Kwords char(15),<br>&nbsp;PRIMARY KEY (KwID));<br><br>&nbsp;and here's the code:<br><br>&nbsp;&lt;?<br>&nbsp;&nbsp;$db = mysql_pconnect(&quot;host&quot;, &quot;username&quot;, &quot;password&quot;);<br>&nbsp;&nbsp;mysql_select_db(&quot;dbasename&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;HTML&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;HEAD&gt;&lt;TITLE&gt;Keywords&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&quot;&lt;BODY&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;'&lt;TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0 WIDTH=468&gt;&lt;TR<br>&nbsp;VALIGN=TOP&gt;&lt;TD&gt;';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;FORM ACTION=\&quot;$PHP_SELF\&quot; METHOD=\&quot;POST\&quot;&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;BR&gt;&lt;INPUT TYPE=\&quot;TEXT\&quot; NAME=\&quot;KeyWords\&quot; SIZE=30&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;INPUT TYPE=\&quot;SUBMIT\&quot; NAME=\&quot;Search\&quot; VALUE=\&quot;Search\&quot;&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;'&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;BR&gt;';<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$my_array = explode(&quot; &quot;, $KeyWords);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0; $i &lt; count($my_array) ; $i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$my_array[$i]&lt;BR&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;if($KeyWords != &quot; &quot;){<br>&nbsp;$count = count($my_array);<br>&nbsp;$sql .= &quot;SELECT * from KeyWords where &quot;;<br>&nbsp;for ($i=0;$i&lt;count($count);$i++) {<br>&nbsp;if ($i == $count) {<br>&nbsp;&nbsp;$sql .= &quot;(Kwords LIKE '%$my_array[$i]%')&quot;;<br>&nbsp;}<br>&nbsp;&nbsp;else {<br>&nbsp;&nbsp;$sql .= &quot;(Kwords LIKE '%$my_array[$i]%') OR &quot;;<br>&nbsp;}<br>&nbsp;}<br>&nbsp;&nbsp;$sql .= &quot;)&quot;;<br><br>&nbsp;$result = mysql_query($sql,$db);<br><br>&nbsp;print &quot;$result&quot;;<br>&nbsp;}<br>&nbsp;&nbsp;print &quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;<br>&nbsp;&nbsp;?&gt;<br><br>&nbsp;If I entered a keyword which is existing in the table, it should display<br>&nbsp;the keyword if its not, it should display nothing.<br><br>&nbsp;I hope you'll be able to help me, thanks in advance...<br><br>&nbsp;PHUTIE<br><br>
 
Ok, this is an example of code that returns all the mathing words (sorry but i did not check your part of the code) :<br><br>&nbsp;&lt;?<br>&nbsp;&nbsp;$db = mysql_pconnect(&quot;host&quot;, &quot;username&quot;, &quot;password&quot;);<br>&nbsp;&nbsp;mysql_select_db(&quot;dbasename&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;HTML&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;HEAD&gt;&lt;TITLE&gt;Keywords&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&quot;&lt;BODY&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;'&lt;TABLE BORDER=0 CELLSPACING=2 CELLPADDING=0 WIDTH=468&gt;&lt;TR<br>&nbsp;VALIGN=TOP&gt;&lt;TD&gt;';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;FORM ACTION=\&quot;$PHP_SELF\&quot; METHOD=\&quot;POST\&quot;&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;BR&gt;&lt;INPUT TYPE=\&quot;TEXT\&quot; NAME=\&quot;KeyWords\&quot; SIZE=30&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;&quot;&lt;INPUT TYPE=\&quot;SUBMIT\&quot; NAME=\&quot;Search\&quot; VALUE=\&quot;Search\&quot;&gt;\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;&nbsp;&nbsp;'&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;BR&gt;';<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$my_array = explode(&quot; &quot;, $KeyWords);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0; $i &lt; count($my_array) ; $i++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;$my_array[$i]&lt;BR&gt;&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;if($KeyWords != &quot; &quot;){<br>&nbsp;$count = count($my_array);<br>&nbsp;$sql .= &quot;SELECT * from KeyWords where &quot;;<br>&nbsp;for ($i=0;$i&lt;count($count);$i++) {<br>&nbsp;if ($i == $count) {<br>&nbsp;&nbsp;$sql .= &quot;(Kwords LIKE '%$my_array[$i]%')&quot;;<br>&nbsp;}<br>&nbsp;&nbsp;else {<br>&nbsp;&nbsp;$sql .= &quot;(Kwords LIKE '%$my_array[$i]%') OR &quot;;<br>&nbsp;}<br>&nbsp;}<br>&nbsp;&nbsp;$sql .= &quot;)&quot;;<br><br>&nbsp;$result = mysql_query($sql,$db);<br><br>&nbsp;$count_result = mysql_affected_rows($db);<br>&nbsp;<br>&nbsp;print(&quot;Query results : &lt;br&gt;&lt;br&gt;\n&quot;);<br>&nbsp;<br>&nbsp; for($i=0; $i&lt;$count_result; $i++) {<br> $row = mysql_fetch_row($result);<br> print(&quot;Result #$i : &lt;b&gt;$row[1]&lt;/b&gt; &lt;br&gt;\n&quot;);<br> }<br>&nbsp;}<br>&nbsp;print &quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;;<br>&nbsp;&nbsp;?&gt; <p>Cédric CHERCHI<br><a href=mailto:pochacco@francemail.com>pochacco@francemail.com</a><br><a href= > </a><br>PHP/MySQL<br>
JavaScript<br>
Flash 4 scripting<br>
HTML/DHTML
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top