9
4

9 回答 9

16

The character ’ is not getting missed, it is simply a character that is not used by mysql for encasing strings, and does not need to be escaped.

The reason it is turning into that strange string is because it is a multi-byte character, and you are putting it into a single byte field.

于 2011-01-19T19:33:21.303 回答
4

您应该使用带有绑定变量的准备好的语句: http: //php.net/manual/en/pdo.prepared-statements.php 这样您就不必担心转义任何内容。我链接到的文档中提到了这些优点。

于 2011-01-19T19:32:27.073 回答
2

mysql_real_escape_string() simply escapes a handful of characters (using \'s) to make them "safe" to shove into your query. You appear to have an encoding mismatch with your data (the styled quote) and your column encoding type. mysql_real_escape_string will never resolve that kind of issue.

于 2011-01-19T19:33:25.920 回答
1

这是一个花哨的报价吗?如果是这样,由于字符编码的差异,它可能在您的数据库中看起来像乱码。每个表都有一个关联的字符编码,连接有自己的编码。

尝试在查询之前执行“SET NAMES utf8”。这会将连接的编码设置为 UTF-8。当然,如果您尝试将 UTF-8 字符存储到 latin1 表中,您仍然不会得到您期望的结果。

于 2011-01-19T19:37:36.420 回答
0

这是一个特殊字符,您需要使用 UTF Encoding

将此行放在要在数据库中插入数据的页面顶部

header ('Content-type: text/html; charset=utf-8');

希望它有效

于 2011-02-03T15:25:06.057 回答
0

如果您已经建立了 mysql 连接,它将起作用: mysql_query ("SET NAMES 'utf8'");

换句话说,如果未设置 SET NAMES 'utf8',则不需要 utf8_encode。

于 2017-03-27T09:04:20.930 回答
-1

更好的是使用 PDO 而不是标准的 mysql。

http://www.php.net/manual/en/class.pdo.php

于 2011-02-06T11:38:38.883 回答
-1
mysql_real_escape_string(utf8_encode($data));

希望这会奏效。

于 2011-02-06T03:28:26.060 回答
-1
<?php
    if(isset($_GET['submit']))
    {
        mysql_connect('localhost','root','');
        mysql_select_db('test');
        $var=mysql_real_escape_string($_GET['asd']);
        $sql="INSERT INTO `test`.`asd` (`id` ,`name` ,`desc`)VALUES ('', '$var', 'knkk');";
        echo $sql."<br />";
        $res=mysql_query($sql) or die('error');
        echo $res;
    }
?>

<html>
<body>
    <form name="f1" method="get">
        <input type="text" name="asd">
        <input type="submit" name="submit">
    </form>
</body>
</html>

输出:

插入到test. asd( id, name, desc)值 ('', 'asd\'lgh', 'knkk');

1

在此处输入图像描述

于 2013-02-13T03:16:03.343 回答