您的位置:首页 > 编程语言 > PHP开发

phpMyAdmin中mysql查询语句中分号注意事项

2012-05-16 19:19 405 查看
最近又在看php,新学者,菜鸟的说》》》》

今天做了一个简单的mysql查询功能,

index.html查询条件页面:

<html> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"> <title>searchpage</title> </head> <body> <formaction="search.php"method="post"> <tablewidth="80%"border="1"align="center"cellpadding="2"bordercolor="#0000FF"bordercolorlight="#7D7DFF"bordercolordark="#0000A0"bgcolor="#E8E8E8"> <tr> <tdwidth="20%"height="29"align="left"valign="middle">装饰理念</td> <tdwidth="88%"height="29"align="left"> <selectname="idea"id="select1"> <optionvalue="实用简约">实用简约</option> <optionvalue="朴素设计">朴素设计</option> </select> </td> </tr> <tr> <tdwidth="20%"height="29"align="left"valign="middle">房屋类型</td> <tdwidth="88%"height="29"align="left"> <selectname="housetype"id="select2"> <optionvalue="二室一厅">二室一厅</option> <optionvalue="三室一厅">三室一厅</option> </select> </td> </tr> <tr> <tdwidth="20%"height="29"align="left"valign="middle">装修风格</td> <tdwidth="88%"height="29"align="left"> <selectname="style"id="select3"> <optionvalue="田园风格">田园风格</option> <optionvalue="中式风格">中式风格</option> </select> </td> </tr> <tr> <tdwidth="20%"height="29"align="left"valign="middle">房屋用途</td> <tdwidth="88%"height="29"align="left"> <selectname="app"id="select4"> <optionvalue="自住">自住</option> <optionvalue="出租">出租</option> </select> </td> </tr> <tr> <tdwidth="20%"height="29"align="left"valign="middle">期望价格</td> <tdwidth="88%"height="29"align="left"> <selectname="price"id="select5"> <optionvalue="A">50000以下</option> <optionvalue="B">50000以上</option> </select> </td> </tr> <tr> <tdwidth="20%"height="29"align="left"valign="middle">发布时间</td> <tdwidth="88%"height="29"align="left"> <selectname="pubdate"id="select6"> <optionvalue="A">最近一周</option> <optionvalue="B">最近一个月</option> <optionvalue="C">最近三个月</option> <optionvalue="D">最近一年</option> </select> </td> </tr> <tr> <td></td> <td><inputtype="submit"value="提交"></td> </tr> </table> </form> </body> </html>

装饰理念实用简约朴素设计
房屋类型二室一厅三室一厅
装修风格田园风格中式风格
房屋用途自住出租
期望价格50000以下50000以上
发布时间最近一周最近一个月最近三个月最近一年
search.php

<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8">
<title>searchresult</title>
</head>

<body>
<tablealign="center">
<?php
$idea=$_POST['idea'];
$housetype=$_POST['housetype'];
$style=$_POST['style'];
$app=$_POST['app'];
$price=$_POST['price'];
$pubdate=$_POST['pubdate'];
#echo$idea,'',$housetype,'',$style,'',$app,'',$price,'',$pubdate;
$conn=mysql_connect("localhost","thoupin","D3w89X7z");
mysql_select_db("sample");
$query="select*from`case`where`idea`='".$idea."'";

#housetype
if($housetype!=""){
$query.="&&`housetype`='".$housetype."'";
}
#style
if($style!=""){
$query.="&&`style`='".$style."'";
}
#app
if($app!=""){
$query.="&&`app`='".$app."'";
}
#price
/*
if($price!=""){
switch($price){
case"A":
$query.="&&`price`<50000";
break;
case"B":
$query.="&&`price`>=50000";
break;
}
}
#pubdate

if($pubdate!=""){
switch($pubdate){
case"A":
$query.="&&TO_DAYS(NOW())-TO_DAYS(`pubdate`)<=7";
break;
case"B":
$query.="&&TO_DAYS(NOW())-TO_DAYS(`pubdate`)<=30";
break;
case"C":
$query.="&&TO_DAYS(NOW())-TO_DAYS(`pubdate`)<=91";
break;
case"D":
$query.="&&TO_DAYS(NOW())-TO_DAYS(`pubdate`)<=183";
break;
}
}
*/
#result
$query.="orderby`pid`limit0,20;";
@mysql_query("setnames'utf8'",$conn);
$result=mysql_query($query,$conn);
if($result){
$rows=mysql_num_rows($result);
if($rows!=0){
while($myrow=mysql_fetch_array($result)){
echo'<tr>';
echo"<tdwidth='15'height='12'><inputname='t1'type='radio'value=''/></td>";
echo"<tdwidth='540'height='12'>
$myrow[pid] 
$myrow[idea] 
$myrow[style]($myrow[housetype]) 
<fontstyle='font-size:9pt'>[$myrow[pubdate]]</font>";
echo'</td>';
echo"<tdwidth='75'height='12'><ahref=''target='_blank'>详细信息</a></td>";
echo'</td>';
}
}
}else{
echo"<tr><td><divalign='center'><br/><br/>没有符合查找条件的记录!</div></td></tr>";
}

echo"<hrwidth=\"100%\"size=\"1\">";
?>
</table>
</body>
</html>


运行的结果为:

  

1实用简约田园风格(二室一厅)[2012-02-1713:23:17]详细信息
注意这一句:

$query="select*from`case`where`idea`='".$idea."'";
当时我是直接一律用的单引号(即"'"),然后就是如下结果:




尔后将单引号改为``后可以得到正确结果:




可见在phpMyAdmin的查询环境(windowsutf-8)中,单引号应为``;

但是绝不是一律使用``,例如下列情况:




  可见,对于条件字符,应使用双引或者单引均可,(而非``):





另外,对于数字,需不需要引号为所谓,但建议加上。

对于表名,列名,应使用``,而对于条件数据,应使用'',或者""。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: