0

Hi, I am using the Like %$search% clause in my query to find results in my database, and I was wondering if this is the most effective and efficent way to do this?

4
Contributors
4
Replies
6
Views
4 Years
Discussion Span
Last Post by diafol
0

The answer is a definite maybe. As far as being effective, if it's not effective then it's not a viable solution because it doesn't work. So there's not really any degree here, it either works for your needs or it doesn't.

Whether it's the most efficient way really depends on what exactly you want to happen. The data might support an exact or even indexed match, and in that case a LIKE clause would be relatively inefficient.

0

It seems to be working fine and having no issues for what I need it to do, but is it possible for me to order the results by the most matched word? For instance the word searched is hat, the words hat and bat show up, is it possible to make sure hat is displayed first?

0

For instance the word searched is hat, the words hat and bat show up, is it possible to make sure hat is displayed first?

You need to used a preg_match() function.

For example

word is column in the database.

When you find a word in $word then used this:

if(preg_match("/[A-Z|a-z]+/", $_POST['word'])){$word=$_POST['word'];} 

Then you SELECT:

$sql="SELECT * FROM mydata WHERE word LIKE '%".$word."%'";

You need to play around with the preg_match() to find that pattern to make hat appear first.

I don't know the pattern.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.