2

I have a search form field that I want to compare with multiple columns within a table e.g.:

+----------+-------+---------+------+------------+
| name     | owner | species | sex  | birth      |
+----------+-------+---------+------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 |
| Bowser   | Diane | dog     | m    | 1989-08-31 |
| Whistler | Gwen  | cat     | m    | 1997-12-09 |
+----------+-------+---------+------+------------+

The query I made has multiple LIKE comparisons however it only seems to find rows of data for the first two columns.

The first comparison is "name" then "owner" then "species" then "sex".

It only finds rows if you enter a string that matches the first two columns, however if you enter a string that matches one of the last columns it does not return anything.

Here is the query:

SELECT * FROM contact 
WHERE name LIKE \"%$search%\" 
OR owner LIKE \"%$search%\"
OR species LIKE \"%$search%\" 
OR sex LIKE \"%$search%\" 
GROUP BY name, owner,species, sex

My only guess is that MySQL perhaps limits how many "OR"s you can use?

My query should return a row from the table if the string entered by the user matches any column data.

Here is my PHP code for it

include("database.class.php");

    //search string assigned from POST field
    $search = $_POST['contact_search'];

    $db = new database();

    $db->select('quote_system');

    $result = $db->query("SELECT * FROM contact WHERE name LIKE \"%$search%\" OR owner LIKE \"%$search%\" OR species LIKE \"%$search%\" OR sex LIKE \"%$search%\" GROUP BY name, second_name, owner, species, sex");

    print_r($result);

    while ($row = $db->fetch_assoc($result))
    {
        echo $row['name'];
        echo $row['owner'];
        echo $row['species'];
        echo $row['sex'];
        echo "<br/>";

    }

Incase theres any confusion here are some examples to show the problem:

User enters string "claws" --> string matches data in "name" column --> prints row that column is in

User enters string "m" --> string only matches data in "sex" column --> nothing shows!

Thanks for your time!

6
  • Just a tip. Don't do this. Your queries have no indexes to use, meaning full table scanes several times. Commented Jan 24, 2012 at 9:57
  • @Layke - im a beginner, what should I do then? Commented Jan 24, 2012 at 9:58
  • There is no limit on OR, and your query should work. However, your PHP example contains a totally different query than your first example (company_name instead of species etc). Try printing your SQL before executing it, you'll see what's wrong fast. Print it, copy paste it into your SQL client, and execute it yourself. Commented Jan 24, 2012 at 10:00
  • Sorry I pasted the wrong SQL - heres is the new one @Konerak Commented Jan 24, 2012 at 10:03
  • @loosebruce: SQL injection. Read about it Commented Jan 24, 2012 at 10:26

1 Answer 1

3

Your query

$result = $db->query("
  SELECT * FROM contact 
  WHERE first_name LIKE \"%$search%\" 
  OR second_name LIKE \"%$search%\" 
  OR company_name LIKE \"%$search%\" 
  OR email LIKE \"%$search%\" 
  GROUP BY first_name, second_name, company_name, email
");

seems to belong to another table. Also, why do you GROUP BY? Maybe, you meant ORDER BY?

Sign up to request clarification or add additional context in comments.

2 Comments

i use groupby to remove duplicate rows returned
Use Select DISTINCT... for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.