dlyles Posted December 10, 2006 Share Posted December 10, 2006 Ok, I would like to do a basic search using a keyword. I'm trying to query the db for a like match, but I think my like statement is wrong.[code]$query="select * from parts where PartName LIKE '%$model%' ";[/code]Any ideas?Also, if someone can point me in a good direction as to doing a better search. Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/ Share on other sites More sharing options...
paul2463 Posted December 10, 2006 Share Posted December 10, 2006 [code]$query="select * from parts where PartName LIKE '%$model%' ";[/code]this is a well formed query, as long as $model is created before the query takes placei used this on one of mine and it works well[code]<?php $text = "Table";$query = "SELECT * FROM products WHERE descProd LIKE '%$text%'";$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());$num = mysql_num_rows($result);echo $num;?>[/code]I got the answer[quote]169[/quote]not that thatmeans a lot to you but it is the right answer for the query I made. so your query is good, as i said as long as $model is set correctlyPaul Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-138501 Share on other sites More sharing options...
.josh Posted December 10, 2006 Share Posted December 10, 2006 [quote]but I think my like statement is wrong.[/quote]why do you think it's wrong? Are you getting any errors? Is the 'wrong' thing returning? Is anything returning at all? Do you even have error reporting on, or displaying errors? Have you echoed out $query and tried inserting it directly into your database and see if it works? In short, simply telling us you think something is 'wrong' does not help us help you whatsoever. Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-138547 Share on other sites More sharing options...
dlyles Posted December 11, 2006 Author Share Posted December 11, 2006 Ok, I worked through this problem, but what I'm wondering is how can I take a phrase like "HP Workstations computer" and look for anything with any word. Not the words as a phrase, but any word or combination. Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139175 Share on other sites More sharing options...
bljepp69 Posted December 11, 2006 Share Posted December 11, 2006 Take a look at this tutorial [url=http://www.phpfreaks.com/tutorials/129/0.php]MySQL Full-Text Searching with PHP[/url] Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139193 Share on other sites More sharing options...
dlyles Posted December 11, 2006 Author Share Posted December 11, 2006 Ok, I've looked. I created my indexes and have the following code (that isn't working). I used the word "work" as a test word to search for.[code] $sql = "SELECT PartName, description MATCH(PartName, description) AGAINST ('work' IN BOOLEAN MODE) AS score FROM parts WHERE MATCH(PartName, description) AGAINST ('work' IN BOOLEAN MODE) ORDER BY score DESC"; [/code] Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139219 Share on other sites More sharing options...
craygo Posted December 11, 2006 Share Posted December 11, 2006 in order to use FullText search you have to flag the field as fulltext. This involves changing the structure of your table. If you have phpmyadmin, the fulltext flag is at the very end.Ray Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139233 Share on other sites More sharing options...
dlyles Posted December 11, 2006 Author Share Posted December 11, 2006 I've done that already. But still... Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139247 Share on other sites More sharing options...
craygo Posted December 11, 2006 Share Posted December 11, 2006 your query is wrong[code]$sql = "SELECT PartName, description FROM tablename WHERE MATCH(PartName, description) AGAINST("work") ORDER BY score DESC";[/code]Ray Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139250 Share on other sites More sharing options...
dlyles Posted December 12, 2006 Author Share Posted December 12, 2006 I did that and get the following:Parse error: syntax error, unexpected T_VARIABLE in /var/opt/data/rgbdata/apache/htdocs/theindex.php on line 5I also put this query into phpmyadmin and got this#1191 - Can't find FULLTEXT index matching the column list I checked and confirmed that the fields have been set for FULLTEXT. any ideas? anybody HELP :( Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139626 Share on other sites More sharing options...
dlyles Posted December 12, 2006 Author Share Posted December 12, 2006 Ok, I've gotten almost where I need to be. Here's the problem. Everything APPEARS to be working but it's only working for one particular search word (I dont understand why) and one result. I'm confused as to why that's happening, so here's what I'm using. Straight for the tutorial on this site.[code]function searchForm() { // Re-usable form // variable setup for the form. $searchwords = (isset($_GET['words']) ? htmlspecialchars(stripslashes($_REQUEST['words'])) : ''); $normal = (($_GET['mode'] == 'normal') ? ' selected="selected"' : '' ); $boolean = (($_GET['mode'] == 'boolean') ? ' selected="selected"' : '' ); echo '<form method="get" action="'.$_SERVER['PHP_SELF'].'">'; echo '<input type="hidden" name="cmd" value="search" />'; echo 'Search for: <input type="text" name="words" value="'.$searchwords.'" /> '; echo 'Mode: '; echo '<select name="mode">'; echo '<option value="normal"'.$normal.'>Normal</option>'; echo '<option value="boolean"'.$boolean.'>Boolean</option>'; echo '</select> '; echo '<input type="submit" value="Search" />'; echo '</form>'; } // Create the navigation switch $cmd = (isset($_GET['cmd']) ? $_GET['cmd'] : ''); switch($cmd) { default: echo '<h1>Search Database!</h1>'; searchForm(); break; case "search": searchForm(); echo '<h3>Search Results:</h3><br />'; $searchstring = mysql_escape_string($_GET['words']); switch($_GET['mode']) { case "normal": $sql = "SELECT PartName, description, MATCH(PartName, description) AGAINST ('$searchstring') AS score FROM parts WHERE MATCH(PartName, description) AGAINST ('$searchstring') ORDER BY score DESC"; break; case "boolean": $sql = "SELECT PartName, description, MATCH(PartName, description) AGAINST ('$searchstring' IN BOOLEAN MODE) AS score FROM parts WHERE MATCH(PartName, description) AGAINST ('$searchstring' IN BOOLEAN MODE) ORDER BY score DESC"; break; } // echo $sql; $result = mysql_query($sql) or die (mysql_error()); while($row = mysql_fetch_object($result)) { echo '<strong>Title: '.stripslashes(htmlspecialchars($row->PartName)).'</strong><br />'; echo 'Score:'. number_format($row->score, 1).' <br />'; echo '<p>'.stripslashes(htmlspecialchars($row->description)).'</p>'; echo '<hr size="1" />'; } break; } ?> </body> </html> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/30130-searching-like/#findComment-139670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.