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
= or contains ?
Moderator: General Moderators
Re: = or contains ?
what you need is LIKE instead of =
Re: = or contains ?
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());Re: = or contains ?
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...socket1 wrote:That code should work.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());
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());
Sven
Re: = or contains ?
Before your $data = mysql_query do this
put:
then copy the output and paste it into the MySQL console and see if it returns anything.
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%' ";Re: = or contains ?
@ziepe, let's start wrapping your code in the code tags.
Re: = or contains ?
The output is empty...socket1 wrote:Before your $data = mysql_query do this
put:then copy the output and paste it into the MySQL console and see if it returns anything.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%' ";
Tomorrow (within a few hours
Thanks,
Sven