Page 1 of 1
= or contains ?
Posted: Mon Mar 16, 2009 3:55 pm
by ziepe
Hello again,
while I'm at it I might ask my last question...
Inside the site there is a very basic search function, and it works really nice. The only problem is that
PHP looks for perfect matches. For example:
I can type "rainbow" and the query will look for rainbow,
but if I type 'rain', no results will be displayed, which is logical hence I use '=' in the mysql_query.
Is there a way to use something like 'contains' instead of '=' ?
Part of the code:
$lookfor = $_POST["name"];
$data = mysql_query("SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus= '$lookfor' OR species= '$lookfor' OR description = '$lookfor' ") or die(mysql_error());
Again, I apologize if my question is really dumb, but I don't seem to be able to find an answer in a short notice;
thanks in advance anyway!
Sven
Re: = or contains ?
Posted: Mon Mar 16, 2009 3:57 pm
by deejay
what you need is LIKE instead of =
Re: = or contains ?
Posted: Mon Mar 16, 2009 4:40 pm
by socket1
Code: Select all
$lookfor = $_POST["name"];
$data = mysql_query("SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus LIKE '%$lookfor%' OR species LIKE '%$lookfor%' OR description LIKE '%$lookfor%' ") or die(mysql_error());
That code should work.
Re: = or contains ?
Posted: Mon Mar 16, 2009 5:21 pm
by ziepe
socket1 wrote:Code: Select all
$lookfor = $_POST["name"];
$data = mysql_query("SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus LIKE '%$lookfor%' OR species LIKE '%$lookfor%' OR description LIKE '%$lookfor%' ") or die(mysql_error());
That code should work.
Well, I tried it before using 'LIKE' and adding % before and after the variable, but it still refuses to work. I get no error message(s), and the mysql_query only executes correctly when I enter 'complete' words. When I enter partially written words the result is an empty page. I'm beginning to wonder whether I did something wrong at the source of the page...
One thing that catches my attention is that my editor (dreamweaver in this case) does not colorizemy code correctly after adding the % before the variable:
Code: Select all
$data = mysql_query("SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus LIKE '%$lookfor%' OR species LIKE '%$lookfor%' OR description LIKE '%$lookfor%' ") or die(mysql_error());
There seems to be problem with '%lookfor... i guess.
Sven
Re: = or contains ?
Posted: Mon Mar 16, 2009 5:25 pm
by socket1
Before your $data = mysql_query do this
put:
Code: Select all
echo "SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus LIKE '%$lookfor%' OR species LIKE '%$lookfor%' OR description LIKE '%$lookfor%' ";
then copy the output and paste it into the MySQL console and see if it returns anything.
Re: = or contains ?
Posted: Mon Mar 16, 2009 5:28 pm
by Benjamin
@ziepe, let's start wrapping your code in the code tags.
Re: = or contains ?
Posted: Mon Mar 16, 2009 5:41 pm
by ziepe
socket1 wrote:Before your $data = mysql_query do this
put:
Code: Select all
echo "SELECT * FROM $dbtable JOIN genera_table ON $dbtable.genus = genera_table.genera_id WHERE genera_table.genus LIKE '%$lookfor%' OR species LIKE '%$lookfor%' OR description LIKE '%$lookfor%' ";
then copy the output and paste it into the MySQL console and see if it returns anything.
The output is empty...
Tomorrow (within a few hours

) I'll check at work with PHPbug firefox and see if there is some output there.
Thanks,
Sven