= or contains ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziepe
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:32 pm

= or contains ?

Post 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
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: = or contains ?

Post by deejay »

what you need is LIKE instead of =
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: = or contains ?

Post 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.
ziepe
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:32 pm

Re: = or contains ?

Post 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
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

Re: = or contains ?

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: = or contains ?

Post by Benjamin »

@ziepe, let's start wrapping your code in the code tags.
ziepe
Forum Newbie
Posts: 6
Joined: Mon Mar 16, 2009 3:32 pm

Re: = or contains ?

Post 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
Post Reply