9 回答
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.
您应该使用带有绑定变量的准备好的语句: http: //php.net/manual/en/pdo.prepared-statements.php 这样您就不必担心转义任何内容。我链接到的文档中提到了这些优点。
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.
这是一个花哨的报价吗?如果是这样,由于字符编码的差异,它可能在您的数据库中看起来像乱码。每个表都有一个关联的字符编码,连接有自己的编码。
尝试在查询之前执行“SET NAMES utf8”。这会将连接的编码设置为 UTF-8。当然,如果您尝试将 UTF-8 字符存储到 latin1 表中,您仍然不会得到您期望的结果。
这是一个特殊字符,您需要使用 UTF Encoding
将此行放在要在数据库中插入数据的页面顶部
header ('Content-type: text/html; charset=utf-8');
希望它有效
如果您已经建立了 mysql 连接,它将起作用: mysql_query ("SET NAMES 'utf8'");
换句话说,如果未设置 SET NAMES 'utf8',则不需要 utf8_encode。
更好的是使用 PDO 而不是标准的 mysql。
mysql_real_escape_string(utf8_encode($data));
希望这会奏效。
<?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