Rumah >pembangunan bahagian belakang >tutorial php >mariadb - php使用带where的SQL查不出数据
先上代码:
<code>php</code><code class="lang-php"><form method="post" action="<?php%20echo%20htmlspecialchars(%24_SERVER%5B" php_self>"> <input name="qname" type="text" class="textlable" id="lname" onfocus="if (value =='姓名'){value ='';}" onblur="if (value == ''){value='姓名';}" value="姓名"> <input name="qcont" type="submit" value="查询"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_POST["qname"] =="姓名") { $nameErr = "请输入姓名"; }else { $con = mysql_connect("localhost","root","123456"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_query("set names ’utf8’ "); mysql_query("set character_set_client=utf8"); mysql_query("set character_set_results=utf8"); mysql_select_db("gkweb", $con); $sql="select c_name,c_extension,c_mail from gk_contacts where c_name='".$_POST["qname"]."'"; echo $sql; $result = mysql_query($sql); echo "<table align='center' class='altrowstable' id='alternatecolor'><tr> <th>姓名</th> <th>座机</th> <th>电子邮件</th> </tr>"; echo 123; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['c_name'] . "</td>"; echo "<td>" . $row['c_extension'] . "</td>"; echo "<td>" . $row['c_mail'] . "</td>"; echo "</tr>"; } echo ""; mysql_close($con); }} ?> </code>
不知道为什么只要使用了带where的sql进行查询就没有输出结果。
但是使用$sql="select c_name,c_extension,c_mail from gk_contacts"却能输出所有数据.
我也使用echo $sql;输出的SQL在数据库后台查询了,能输出结果。求大神告知是哪里写错了。
不知道怎么排版,求不嫌弃。谢谢
刚才有大神说可能是数据库字符编码的问题,可我数据库都是用的UTF8
<code>bash</code><code class="lang-bash">MariaDB [(none)]> show variables like '%char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) </code>
先上代码:
<code>php</code><code class="lang-php"><form method="post" action="<?php%20echo%20htmlspecialchars(%24_SERVER%5B" php_self>"> <input name="qname" type="text" class="textlable" id="lname" onfocus="if (value =='姓名'){value ='';}" onblur="if (value == ''){value='姓名';}" value="姓名"> <input name="qcont" type="submit" value="查询"> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { if ($_POST["qname"] =="姓名") { $nameErr = "请输入姓名"; }else { $con = mysql_connect("localhost","root","123456"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_query("set names ’utf8’ "); mysql_query("set character_set_client=utf8"); mysql_query("set character_set_results=utf8"); mysql_select_db("gkweb", $con); $sql="select c_name,c_extension,c_mail from gk_contacts where c_name='".$_POST["qname"]."'"; echo $sql; $result = mysql_query($sql); echo "<table align='center' class='altrowstable' id='alternatecolor'><tr> <th>姓名</th> <th>座机</th> <th>电子邮件</th> </tr>"; echo 123; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['c_name'] . "</td>"; echo "<td>" . $row['c_extension'] . "</td>"; echo "<td>" . $row['c_mail'] . "</td>"; echo "</tr>"; } echo ""; mysql_close($con); }} ?> </code>
不知道为什么只要使用了带where的sql进行查询就没有输出结果。
但是使用$sql="select c_name,c_extension,c_mail from gk_contacts"却能输出所有数据.
我也使用echo $sql;输出的SQL在数据库后台查询了,能输出结果。求大神告知是哪里写错了。
不知道怎么排版,求不嫌弃。谢谢
刚才有大神说可能是数据库字符编码的问题,可我数据库都是用的UTF8
<code>bash</code><code class="lang-bash">MariaDB [(none)]> show variables like '%char%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) </code>
mysql_query("set names ’utf8’ "); 改成mysql_query("set names utf8 ")
先确认一下记录是否存在或者qname
是否拼写错误。如果都无误的话。那应该就是数据库表的编码问题了。