Home  >  Article  >  Backend Development  >  How to query the database with multiple conditions in php

How to query the database with multiple conditions in php

藏色散人
藏色散人Original
2021-09-16 10:07:283557browse

How to implement multi-condition query database in php: 1. Make a form and create a table to display the table; 2. Call the encapsulation class, create the corresponding conditions, and check whether the data is empty; 3. Just use php code to traverse and query the tables of the database.

How to query the database with multiple conditions in php

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer

How to query the database with multiple conditions in php?

Multi-condition query:

1. First make a form and create a table to display the table:

<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <td>代号</td>
    <td>名称</td>
    <td>系列</td>
    <td>上市时间</td>
    <td>价格</td>
</tr>
</table>

2. Call the encapsulation class, create corresponding conditions, and check whether the data is empty:

<?php
require "DBDA.class.php";
$db=new DBDA();//1.如果没有提交数据,显示所有//2.如果有提交数据,根据关键字查询显示$name="";
$tj1=" 1=1 "; //第一个条件,对应名称,要用空格隔开$tj2=" 1=1 ";//第二个条件,对应系列,要用空格隔开if(!empty($_POST["name"]))
{
    $name = $_POST["name"];
    $tj1 = " name like &#39;%{$name}%&#39; ";
}if(!empty($_POST["brand"]))
{
    $brand = $_POST["brand"];
    $tj2 = " brand = &#39;{$brand}&#39; ";
}//总条件$tj=" {$tj1}and{$tj2} ";
$sql="select * from car where".$tj;?>

3. Use PHP code to traverse the database table and add the keywords Turns red (php should be embedded in table):

<?php
$arr = $db->query($sql);foreach($arr as $v)
{
    $str = str_replace($name,"<span style=&#39;color:red&#39;>{$name}</span>",$v[1]);
    echo "<tr>
    <td>{$v[0]}</td>
    <td>{$str}</td>
    <td>{$v[2]}</td>
    <td>{$v[3]}</td>
    <td>{$v[7]}</td>
</tr>";}?>

The final result is:

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query the database with multiple conditions in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn