Home > Backend Development > PHP Tutorial > PHP example-php implements query function (data access)

PHP example-php implements query function (data access)

微波
Release: 2023-03-11 18:56:02
Original
1487 people have browsed it

This article mainly introduces how to implement the query function in PHP and access data. It has certain reference value. Interested friends can refer to it.

The example in this article shares with you how to implement the query function in PHP. The specific code is for your reference. The specific content is as follows

Enter keyword effect

Code:

<body>
<h1>汽车信息</h1>
<?php
require"DBDA.class1.php";//$type 代表SQL语句的类型,0代表增删改,1代表查询
$db = new DBDA();

//如果没有提交数据,显示所有
//如果有提交数据,根据关键字查询显示
$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 like &#39;%{$brand}%&#39;";
}
//总条件
$tj ="{$tj1} and {$tj2}";
$sql = "select * from car where ".$tj;//注意where后空格
/*echo $sql;*/
?>
<form action="chaxun.php" method="post">
<p>名称:<input type="text" name="name" value="<?php echo $name ?>"/>系列:<input type="text" name="brand"/><input type="submit" value="查询" /></p>
</form>
<br/>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td>代号</td>
    <td>名称</td>
    <td>系列</td>
    <td>上市时间</td>
    <td>价格</td>
  </tr>
<?php

//只适用于单条件查询
/*$sql = "select * from car";
$name="";
if(!empty($_POST["name"]))
{
  $name = $_POST["name"];
  $sql = "select * from car where name like &#39;%{$name}%&#39; ";
}*/

$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>";
  }
?>
</table>
</body>
</html>
Copy after login

Referenced encapsulation class

<?php
class DBDA
{
  public $host = "localhost";
  public $uid = "root";
  public $pwd = "123";
  public $dbname = "newssystem";
  //执行SQL语句返回相应的结果
  //$sql 要执行的SQL语句
  //$type 代表SQL语句的类型,0代表增删改,1代表查询
  function query($sql,$type=1)
  {
    $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
    
    $result = $db->query($sql);
    
    if($type)
    {
      //如果是查询,显示数据
      return $result->fetch_all();
    }
    else
    {
      //如果是增删改,返回true或者false
      return $result;
    }
  }
}
Copy after login

The above is the detailed content of PHP example-php implements query function (data access). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template