数据的查询(一)——单条件查询

数据的查询分为但条件查询和多条件查询

在查询之前,将查询数据库进行封装

<?php
class DBDA
{
	public $host="localhost";  //数据库地址
	public $uid = "root";  //数据库用户名
	public $pwd = "";   //密码
	
	//执行sql语句,返回相应的结果
	//参数:$sql代表执行的sql语句;$type是sql语句类型0代表查询,1代表其他;$db代表操作的数据库
	public function Query($sql,$type=0,$db="mydb")
	{
		//1.连接数据库
		$dbconnect=new MySQLi($this->host,$this->uid,$this->pwd,$db);
		//2.判断是否出错
		!mysqli_connect_error() or die("连接失败!");
		//3.执行sql语句
		$result=$dbconnect->query($sql);
		
		if($type==0)
		{
			return $result->fetch_all();
		}
		else
		{
			return $result;
		}	
	} 	
}

  

单条件查询

   以汽车数据库为例

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库查询</title>
</head>

<body>
<?php
     include("DBDA.class.php");
		 $db=new DBDA();
		 
		 $cx="";
		 $value="";
		 if(!empty($_POST["name"]))  //里边内容不为空
		 {
			 $name=$_POST["name"];
		     $cx=" where name like '%{$name}%'";  //查询字符串
			 $value=$name;
		 }
?>
<h1>汽车查询页面</h1><br />
<form action="test.php" method="post">
<div>
    请输入名称:<input type="text" name="name" value="<?php echo $value;?>"/> 
    <input type="submit" value="查询"/>
</div></form>
<br />

<table width="1000px" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td>代号</td>
        <td>汽车名称</td>
        <td>价格</td>
        <td>油耗</td>
        <td>功率</td>
    </tr>
    
    <?php
	     
		 
		 
		 $sql="select * from car".$cx;
		 $attr=$db->Query($sql);
		 
		 foreach($attr as $v)
		 {
			 //处理name
			 $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[7]}</td>
					 <td>{$v[4]}</td>
					 <td>{$v[5]}</td>
			       </tr>";
		 }
	
    ?>
</table>
</body>
</html>

 

  

查询结果显示页面

 

转载于:https://www.cnblogs.com/zst062102/p/5469407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值