Home > Backend Development > PHP Tutorial > mariadb - php使用带where的SQL查不出数据

mariadb - php使用带where的SQL查不出数据

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:30:00
Original
1316 people have browsed it

先上代码:

<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>
Copy after login
Copy after login

不知道为什么只要使用了带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>
Copy after login
Copy after login

回复内容:

先上代码:

<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>
Copy after login
Copy after login

不知道为什么只要使用了带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>
Copy after login
Copy after login

mysql_query("set names ’utf8’ "); 改成mysql_query("set names utf8 ")

先确认一下记录是否存在或者qname是否拼写错误。如果都无误的话。那应该就是数据库表的编码问题了。

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template