Jump to content

Searching LIKE %


dlyles

Recommended Posts

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.
Link to comment
Share on other sites

[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 place

i 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 correctly

Paul
Link to comment
Share on other sites

[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. 
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 5

I 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 :(
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.