1

I am wanting to search more than column from one database with PHP and MySQL. Currently I have this working.

This is my form:

  <form action="products.php" method="POST">
  Search: <input type="text" name="search" />
  <input type="submit" value="submit"/>
  </form>

and here is my current working query:

$query = "SELECT * FROM `GraphicsCards` WHERE `Brand` LIKE '%". $search . "%'";
$run = mysqli_query ($connection, $query);

As you can see my query is searching my database to see if the user search entered matches anything in the column "Brand".

I have other columns such as "Model" "Price" etc etc, and would like to be able to search those as well as just "Brand". Is it possible to do this is one query?

In addition I have already tried ' AND ' and ' OR ' and also ' || ' but they do not work.

3
  • Why doesn't OR work? Commented Jan 8, 2015 at 22:09
  • I don't know when I add OR the query no longer displays data :/ Commented Jan 8, 2015 at 22:10
  • Make sure $search is escaped correctly if it comes from user input, otherwise you'll have a SQL injection vulnerability. It is best to switch to parameterised queries if you can, for this reason. Commented Jan 16, 2015 at 21:31

2 Answers 2

6

OR will work, you just have to do it correctly.

$query = "SELECT * FROM `GraphicsCards`
    WHERE `Brand` LIKE '%". $search . "%'
        OR `Model` LIKE '%" . $search . "%'
        OR `Price` LIKE '%" . $search . "%'";

And just FYI, Brand, Model, and Price are columns, not rows.

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

4 Comments

Patrick you wonderful guy!! It works! Thank you and God Bless :)
@AbdushSamadMiah You're welcome. If you could just mark the answer as accepted since it solved your problem, that would be appreciated.
lool and cheers for the FYI above, I shall remember for future reference :)
Just waiting for it to allow me and will do as soon as it does. Cheers
1

You should try:

$query = "
    SELECT * FROM `GraphicsCards`
    WHERE
        `Brand` LIKE '%". $search . "% OR `Model` LIKE %'" . $search . "%";
$run = mysqli_query ($connection, $query);

Let me know how it works.

1 Comment

Hi Asaf thanks for the response Patrick's answer above resolved my issue I appreciate your response. Cheers

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.