Table of Contents
php数据访问之查询关键字,php数据关键字
汽车查询页面
Home php教程 php手册 php数据访问之查询关键字,php数据关键字

php数据访问之查询关键字,php数据关键字

Jun 13, 2016 am 08:40 AM
php Keywords data access Inquire

php数据访问之查询关键字,php数据关键字

本文实例为大家分享了php查询操作的实现代码,供大家参考,具体内容如下

一、一个关键字查询

主页面:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>汽车查询页面</title>
</head>
 
<body>
<h1 id="汽车查询页面">汽车查询页面</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

封装类: 

<&#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

运行结果:

二、多个关键字查询

主页面:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>汽车查询页面</title>
</head>
 
<body>
<h1 id="汽车查询页面">汽车查询页面</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

运行结果:

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles