PHP searching multiple rows in database

故事扮演 提交于 2020-01-14 03:14:29

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27850383/php-searching-multiple-rows-in-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!