简体   繁体   English

PHP MySQL语句中的特殊字符

[英]Special characters in PHP MySQL statement

I have some special characters stored in a MySQL database. 我在MySQL数据库中存储了一些特殊字符。 Specifically ®. 特别是®。 I want to SELECT all entries with these characters. 我想用这些字符选择所有条目。 I am currently using this statement: 我目前正在使用以下语句:

mysql_query("SELECT * FROM table WHERE description LIKE '%®%' OR name LIKE '%®%'"); mysql_query(“ SELECT * FROM table WHERE description LIKE'%®%'或名称LIKE'%Â%%'”);

It returns 0 entries. 它返回0个条目。 When i try something with %a% it returns a lot so I know everything is right, it is just some kind of a charset problem or something. 当我尝试使用%a%进行处理时,它会返回很多信息,所以我知道一切正确,这只是某种字符集问题或其他原因。

When I echo "®" it returns "å¨". 当我回显“®”时,它返回“娔。

Also when I do the exact same query in phpmyadmin it works properly. 另外,当我在phpmyadmin中执行完全相同的查询时,它也可以正常工作。 Please help! 请帮忙!

Read this its very help full to you 阅读本手册对您有很大帮助

Just simply add those symbols to your text, and execute it as SQL query: 只需将这些符号添加到您的文本中,然后将其作为SQL查询执行即可:

INSERT INTO tbl_name VALUES ("Here's my text: ©®");

When you want to display it one the website don't do anything with these symbols (but remember to escape at least <, >, & (using htmlspecialchars()) cause those has special meaning in XML/SGML (HTML) documents) 当您要显示它时,网站不会对这些符号进行任何操作(但请记住至少要转义<,>和&(使用htmlspecialchars()),因为它们在XML / SGML(HTML)文档中具有特殊含义)

PS. PS。 Also remember to escape text passed to SQL query using mysql_real_escape_string() to avoid any SQL Injection problems. 还请记住使用mysql_real_escape_string()对传递给SQL查询的文本进行转义,以避免任何SQL注入问题。 If your server has magic_quotes_gpc enabled disable it or at least filter your GET/POST/COOKIE data to its raw value. 如果您的服务器启用了magic_quotes_gpc,则将其禁用,或者至少将GET / POST / COOKIE数据过滤为其原始值。 You should always consciously escape values. 您应该始终自觉地逃避价值观。 EDIT: 编辑:

According to your comment... I don't remember whether magic_quotes_gpc are enabled by default but you can easily undone magic quotes effect. 根据您的评论...我不记得默认情况下是否启用了magic_quotes_gpc,但是您可以轻松撤消魔术引号效果。 Just on the very beginning of your PHP code add something like this: 在您的PHP代码的开头,添加如下内容:

if (get_magic_quotes_gpc()) {
  array_walk_recursive($_GET, 'stripslashes');
  array_walk_recursive($_POST, 'stripslashes');
  array_walk_recursive($_COOKIE, 'stripslashes');
}

Now each GPC value should be always raw - without quotes - so you have to escape it manually before passing any variable into query. 现在,每个GPC值都应该始终是原始值-不带引号-因此您必须在将任何变量传递到查询之前手动对其进行转义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM