Home > Backend Development > PHP Tutorial > PHP data access query keywords, PHP data keywords_PHP tutorial

PHP data access query keywords, PHP data keywords_PHP tutorial

WBOY
Release: 2016-07-12 08:52:47
Original
1206 people have browsed it

Query keywords for php data access, php data keywords

The example in this article shares the implementation code of php query operation for your reference. The specific content is as follows

1. One keyword query

Main page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>汽车查询页面</title>
</head>
 
<body>
<h1>汽车查询页面</h1>
<&#63;php
  include("QiChe.class.php");
  $db=new QiChe();
  //保留输入查询的内容
  $cx="";
  $value="";
  if(!empty($_POST["name"]))//判断查询内容是否为空
  {
  $name=$_POST["name"];
  $cx=" where name like '%{$name}%'";//查询的字符串
  $value=$name;
  }
&#63;>
 
<br>
<form action="QiChe.php" method="post">
<div>
请输入查询内容:<input type="text" name="name" value="<&#63;php echo $value; &#63;>"/> 
<input type="submit" value="查询"/>
</div>
</form>
<br />
 
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代号</td>
<td>汽车名称</td>
<td>油耗</td>
<td>功率</td>
<td>价格</td>
</tr>
 
<&#63;php
  $sql="select * from Car".$cx;
  $attr=$db->query($sql);
  foreach ($attr as $v)
  {
  //使输入查询的关键字变色,处理name
  //$rp="<mark>{$value}</mark>";
  $rp="<span style='color:red'>{$value}</span>";
  $arr=str_replace($value,$rp,$v[1]);
   
  echo "<tr>
  <td>{$v[0]}</td>
  <td>{$arr}</td>
  <td>{$v[4]}</td>
  <td>{$v[5]}</td>
  <td>{$v[7]}</td>    
  </tr>";
  }
&#63;>
 
</table>
</body>
</html>
Copy after login

Packaging class:

<&#63;php
class QiChe
{
  public $localhost="localhost";//服务器
  public $uid="root";//用户名
  public $password="";//密码
  //执行查询语句sql方法:
  //参数的含义:$sql代表要执行的sql语句;$type代表sql语句的类型,自义0为查询,1为其他(增删改查);$db代表要查询的数据库
  public function Query($sql,$type="0",$db="mydb")
  {
    $dbconnect=new MySQLi($this->localhost,$this->uid,$this->password,$db);
    !mysqli_connect_error() or die("连接失败 !");
    $result=$dbconnect->query($sql);
     
    if($type==0)
    {
      return $result->fetch_all();
    }
    else
    {
      return $result;
    }
  } 
}
Copy after login

Run result:

2. Multiple keyword query

Main page:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>汽车查询页面</title>
</head>
 
<body>
<h1>汽车查询页面</h1>
<br>
<&#63;php
include ("./DBDA.class.php");
$db=new DBDA();
$cx="";
$value="";
$value1="";
$tj1=" 1=1";//条件1的判断name
$tj2=" 1=1";//条件2的判断brand
if(!empty($_POST["name"]))
{
  $name=$_POST["name"];
  $tj1="name like '%{$_POST['name']}%'"; 
  $value=$name;
   
}
if(!empty($_POST["brand"]))
{
  $name1=$_POST["brand"];
  $tj2="brand= '{$_POST['brand']}'"; 
  $value1=$name1;
}
$cx=" where $tj1 and $tj2";//查询字符串
&#63;>
 
<form action="ChaXun1.php" method="post">
<div>
请输入名称:<input type="text" name="name" value="<&#63;php echo $value; &#63;>"/> 
系列:<input type="text" name="brand" value="<&#63;php echo $value1; &#63;>">
<input type="submit" name="" value="查询">
 
</div>
</form>
<br>
 
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代号</td>
<td>汽车名称</td>
<td>系列</td>
<td>价格</td>
<td>油耗</td>
<td>功率</td>
</tr>
 
<&#63;php
$sql="select * from Car".$cx;
$attr=$db->Query($sql);
foreach ($attr as $v)
{
   
  //处理name
  //$rp="<mark>{$value}</mark>";
  $rp="<span style='color:red'>{$value}</span>";
  $str=str_replace($value,$rp,$v[1]);
  echo "<tr>
  <td>{$v[0]}</td>
  <td>{$str}</td>
  <td>{$v[2]}</td>
  <td>{$v[7]}</td>
  <td>{$v[4]}</td>
  <td>{$v[5]}</td>  
  </tr>";
  }
&#63;>
</table>
 
</body>
</html>
Copy after login

Run result:

The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1125895.htmlTechArticleQuery keywords for php data access, php data keywords. This example shares the implementation code of php query operation. , for your reference, the specific content is as follows: 1. A keyword query...
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