PHP searching multiple rows in database

2019-05-31 22:35发布

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.

2条回答
▲ chillily
2楼-- · 2019-05-31 23:13

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.

查看更多
Lonely孤独者°
3楼-- · 2019-05-31 23:38

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.

查看更多
登录 后发表回答