php数据库多条件查询 php 多条件查询
php 多条件查询数据库MySQL
SELECT * FROM `表名` WHERE bookid=22
成都创新互联公司专注于企业全网营销推广、网站重做改版、长洲网站定制设计、自适应品牌网站建设、H5页面制作、电子商务商城网站建设、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为长洲等各大城市提供网站开发制作服务。
获取BOOKID为22的记录,输出$uid=
if(strpos($uid,"2")!==false):echo"包含";else:echo"不包含";endif;
判断UID中是否含有2
MYSQLI方法统计数量
$num=mysqli_num_rows(mysqli_query($mysqli,"SELECT id FROM `表名` where pinglun=1"));
统计该表中pinglun=1的记录数量
php多条件复合查询
在我们的网站设计过程中,经常会用到多条件查询,本文的源码是一个二手房屋查询的例子。在本例中,我们要实现能够通过地理位置,物业类型,房屋价格,房屋面积及信息发布日期等多个条件查询到客户所需的资料。
查询文件(search.php)
一、生成查询语句:
?
$conn=mysql_connect("localhost","root","");
$db=mysql_select_db("lingyun");
$query="select * from message where tradetype='".$tradetype."'"; //交易类型,如出租,出售
$SQL=$SQL . "wuye='" . $wuye . "'";
if($housetype!="不限"){
$query.=" housetype='".$housetype."'"; //房屋类型,如二室一厅,三室二厅
}
if($degree!="不限"){
$query.=" degree='".$degree."'"; //新旧程度
}
if($wuye!="不限"){
$query.=" wuye='".$wuye."'"; //物业类型 如住房,商铺
}
if($price2!=""){
switch($price1){
case "大于":
$query.=" price'".$price2."'"; //价格
break;
case "等于":
$query.=" price='".$price2."'";
break;
case "小于":
$query.=" price'".$price2."'";
break;
}
}
if($area2!=""){
switch($area1){
case "大于":
$query.=" area'".$area2."'"; //面积
break;
case "等于":
$query.=" area='".$area2."'";
break;
case "小于":
$query.=" area'".$area2."'";
break;
}
}
switch($pubdate){ //发布日期
case "本星期内":
$query.=" TO_DAYS(NOW()) - TO_DAYS(date)=7";
break;
case "一个月内":
$query.=" TO_DAYS(NOW()) - TO_DAYS(date)=30";
break;
case "三个月内":
$query.=" TO_DAYS(NOW()) - TO_DAYS(date)=91";
break;
case "六个月内":
$query.=" TO_DAYS(NOW()) - TO_DAYS(date)=183";
break;
}
if($address!=""){
$query.=" address like '%$address%'"; //地址
}
if(!$page){
$page=1;
}
?
二、输出查询结果:
?php
if ($page){
$page_size=20;
$result=mysql_query($query);
#$message_count=mysql_result($result,0,"total");
$message_count=10;
$page_count=ceil($message_count/$page_size);
$offset=($page-1)*$page_size;
$query=$query." order by date desc limit $offset, $page_size";
$result=mysql_query($query);
if($result){
$rows=mysql_num_rows($result);
if($rows!=0){
while($myrow=mysql_fetch_array($result)){
echo "tr";
echo "td width='15' height='12'img src='image/home2.gif' width='14' height='14'/td";
echo "td width='540' height='12'$myrow[id] $myrow[tradetype] $myrow[address] $myrow[wuye]($myrow[housetype])font style='font-size:9pt'[$myrow[date]]/font";
echo "/td";
echo "td width='75' height='12'a href='view_d.php?code=$myrow[code]' target='_blank'详细内容/a/td";
echo "/tr";
}
}
else echo "trtddiv align='center'img src='image/sorry.gif'brbr没有找到满足你条件的记录/div/td/tr";
}
$prev_page=$page-1;
$next_page=$page+1;
echo "div align='center'";
echo " 第".$page."/".$page_count."页nbsp";
if ($page=1){
echo "|第一页|";
}
else{
echo "a href='$PATH_INFO?page=1'|第一页|/a";
}
echo " ";
if ($prev_page1){
echo "|上一页|";
}
else{
echo "a href='$PATH_INFO?page=$prev_page'|上一页|/a";
}
echo " ";
if ($next_page$page_count){
echo "|下一页|";
}
else{
echo "a href='$PATH_INFO?page=$next_page'|下一页|/a";
}
echo " ";
if ($page=$page_count){
echo "|最后一页|";
}
else{
echo "a href='$PATH_INFO?page=$page_count'|最后一页|/a";
}
echo "/div";
}
else{
echo "p align='center'现在还没有房屋租赁信息!/p";
}
echo "hr width="100%" size="1"";
?
/table
php+mysql多条件多值查询
PHP+Mysql多条件多值查询示例代码:
index.html代码:
!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
html
head
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
titleSQL多条件查询示例/title
/head
body
form method="post" action="deal.php"
h1房屋出租/h1
房屋类型:select name="type"
option value="1"一居室/option
option value="2"二居室/option
option value="3"三居室/option
/selectbr/
面积:input name="area" type="text"/br/
地址:input name="addr" type="text"/br/
input name="btn" type="submit" value="搜索" /br/
/form
/body
/html
deal.php文件:
?php
//连接数据库
$conn=mysql_connect("localhost","root","");
//选择数据库
$db=mysql_select_db("数据库名");
//接收 参数
$type=$_POST['type'];
$area=$_POST['area'];
$addr=$_POST['addr'];
//SQL语句主题
$query="select * from room where ";
//根据条件和传的值拼接sql语句
//判断面积不为空
if($type!=""){
//然后根据具体面积分情况拼接
switch($type){
case 1:
//一居室
$query.=" room_type=1";
break;
case 2:
$query.=" room_type=2";
break;
case 3:
$query.=" room_type=3";
break;
}
}
//面积
if($area!=""){
$query.=" and area ={$area}";
}
//地址
if($addr!=""){
$query.=" and addr like '%{$addr}%'"; //地址
}
//执行查询
$result=mysql_query($query);
//遍历结果
echo "搜搜结果如下:";
while($row=mysql_fetch_array($result)){
echo "地址:".$row['addr'];
echo "br/";
echo "面积:".$row['area'];
echo "br/";
echo "居室:".$row['type'];
echo "br/";
echo "价格:".$row['addr'];
echo "br/";
//等等
}
?
分享标题:php数据库多条件查询 php 多条件查询
URL网址:http://myzitong.com/article/ddjijpp.html