phpMyAdmin中mysql查询语句中分号注意事项
最近又在看php , 新学者, 菜鸟的说》》》》
今天做了一个简单的mysql查询功能,
index.html 查询条件页面:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>search page</title> </head> <body> <form action="search.php" method="post"> <table width="80%" border="1" align="center" cellpadding="2" bordercolor="#0000FF" bordercolorlight="#7D7DFF" bordercolordark="#0000A0" bgcolor="#E8E8E8"> <tr> <td width="20%" height="29" align="left" valign="middle">装饰理念</td> <td width="88%" height="29" align="left"> <select name="idea" id="select1"> <option value="实用简约">实用简约</option> <option value="朴素设计">朴素设计</option> </select> </td> </tr> <tr> <td width="20%" height="29" align="left" valign="middle">房屋类型</td> <td width="88%" height="29" align="left"> <select name="housetype" id="select2"> <option value="二室一厅">二室一厅</option> <option value="三室一厅">三室一厅</option> </select> </td> </tr> <tr> <td width="20%" height="29" align="left" valign="middle">装修风格</td> <td width="88%" height="29" align="left"> <select name="style" id="select3"> <option value="田园风格">田园风格</option> <option value="中式风格">中式风格</option> </select> </td> </tr> <tr> <td width="20%" height="29" align="left" valign="middle">房屋用途</td> <td width="88%" height="29" align="left"> <select name="app" id="select4"> <option value="自住">自住</option> <option value="出租">出租</option> </select> </td> </tr> <tr> <td width="20%" height="29" align="left" valign="middle">期望价格</td> <td width="88%" height="29" align="left"> <select name="price" id="select5"> <option value="A">50000以下</option> <option value="B">50000以上</option> </select> </td> </tr> <tr> <td width="20%" height="29" align="left" valign="middle">发布时间</td> <td width="88%" height="29" align="left"> <select name="pubdate" id="select6"> <option value="A">最近一周</option> <option value="B">最近一个月</option> <option value="C">最近三个月</option> <option value="D">最近一年</option> </select> </td> </tr> <tr> <td> </td> <td> <input type="submit" value="提交"> </td> </tr> </table> </form> </body> </html>
装饰理念 | |
房屋类型 | |
装修风格 | |
房屋用途 | |
期望价格 | |
发布时间 | |
search.php
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 4 <title>search result</title> 5 </head> 6 7 <body> 8 <table align="center"> 9 <?php 10 $idea = $_POST['idea']; 11 $housetype = $_POST['housetype']; 12 $style = $_POST['style']; 13 $app = $_POST['app']; 14 $price = $_POST['price']; 15 $pubdate = $_POST['pubdate']; 16 # echo $idea, ' ', $housetype, ' ', $style, ' ', $app, ' ', $price, ' ', $pubdate; 17 $conn = mysql_connect("localhost", "thoupin", "D3w89X7z"); 18 mysql_select_db("sample"); 19 $query = "select * from `case` where `idea`='".$idea."'"; 20 21 # housetype 22 if($housetype!="") { 23 $query.=" && `housetype`='".$housetype."'"; 24 } 25 # style 26 if($style!="") { 27 $query.=" && `style`='".$style."'"; 28 } 29 # app 30 if($app!="") { 31 $query.=" && `app`='".$app."'"; 32 } 33 # price 34 /* 35 if($price!="") { 36 switch($price) { 37 case "A": 38 $query.=" && `price`<50000"; 39 break; 40 case "B": 41 $query.=" && `price`>=50000"; 42 break; 43 } 44 } 45 # pubdate 46 47 if($pubdate!="") { 48 switch($pubdate) { 49 case "A": 50 $query.=" && TO_DAYS(NOW()) - TO_DAYS(`pubdate`)<=7"; 51 break; 52 case "B": 53 $query.=" && TO_DAYS(NOW()) - TO_DAYS(`pubdate`)<=30"; 54 break; 55 case "C": 56 $query.=" && TO_DAYS(NOW()) - TO_DAYS(`pubdate`)<=91"; 57 break; 58 case "D": 59 $query.=" && TO_DAYS(NOW()) - TO_DAYS(`pubdate`)<=183"; 60 break; 61 } 62 } 63 */ 64 # result 65 $query.=" order by `pid` limit 0, 20;"; 66 @mysql_query("set names 'utf8'", $conn); 67 $result = mysql_query($query, $conn); 68 if($result) { 69 $rows = mysql_num_rows($result); 70 if($rows != 0) { 71 while($myrow=mysql_fetch_array($result)) { 72 echo '<tr>'; 73 echo "<td width='15' height='12'><input name='t1' type='radio' value=''/></td>"; 74 echo "<td width='540' height='12'> 75 $myrow[pid] 76 $myrow[idea] 77 $myrow[style]($myrow[housetype]) 78 <font style='font-size:9pt'>[$myrow[pubdate]]</font>"; 79 echo '</td>'; 80 echo "<td width='75' height='12'><a href='' target='_blank'>详细信息</a></td>"; 81 echo '</td>'; 82 } 83 } 84 } else { 85 echo "<tr><td><div align='center'><br/><br/>没有符合查找条件的记录!</div></td></tr>"; 86 } 87 88 echo "<hr width=\"100%\" size=\"1\">"; 89 ?> 90 </table> 91 </body> 92 </html>
运行的结果为:
1 实用简约 田园风格(二室一厅) [2012-02-17 13:23:17] | 详细信息 |
注意这一句:
$query = "select * from `case` where `idea`='".$idea."'";
当时我是直接一律用的单引号 (即" ' "), 然后就是如下结果:
尔后将单引号改为`` 后可以得到正确结果:
可见在phpMyAdmin 的查询环境(windows utf-8)中,单引号应为``;
但是绝不是一律使用``, 例如下列情况:
可见, 对于条件字符,应使用双引或者单引 均可,(而非``):
另外,对于数字, 需不需要引号为所谓, 但建议加上。
对于表名,列名, 应使用`` , 而对于条件数据, 应使用'', 或者""。