如何用ñ显示数据库值 [英] How to display database value with ñ

查看:107
本文介绍了如何用ñ显示数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多类型的解决方案,如:



- htmlentities - >使用这个时,所有单词都ñ不会显示



- htmlspecialchars - >所有单词都会有?



- utf8_encode - >所有单词都有ñ将是?



- html_entity_decode(htmlentities($ test)); - >使用这个时,所有单词都不会显示



我的代码只是一个简单的选择,这是我的代码:

  if(isset($ _ GET ['cityname1'] )
{

$ cityname = strval($ _ GET ['cityname1']);
$ cname = mysql_query(SELECT cityname FROM city WHERE provinceid ='$ cityname'ORDER BY cityname ASC);

echo< option value ='0'>选择区域< / option>;

while($ provincecerow = mysql_fetch_array($ cname))
{
$ pname = htmlentities($ provincecerow ['cityname']);
echo< option value ='{$ pname}'> {pname}< / option>;
}
}
else
{
echo请联系技术团队;
}


解决方案

啊,你已经绊倒了浏览器,PHP和MySQL中字符编码的奇妙世界。



处理这些字符不是一件微不足道的事,因为它依赖于许多因素。通常,PHP和MySQL之间的通信不在UTF-8中,这意味着特殊字符(如ñ)会混乱。因此,将连接设置为UTF-8是一个很好的开始。此外,可能是PHP不以符合UTF-8的方式运行,可以使用这里



当这个设置被正确设置时,你应该可以使用html_entities函数来正确地将字符替换为HTML字符编码(ñ)。



不同服务(如PHP和MySQL)之间的通信的主要问题是当他们不使用相同的字符编码时,字符(基本上是数字)将被混乱。由于MySQL和PHP都将为某个字符使用不同的数字。对于非特殊字符(如非重​​音字母表),这是非常标准化的,但是对于更奇怪的字符,您仍然会遇到一些困难。



<请注意,这个答案假设一个基本的设置,如果我做出了一个不合理的假设,请提供反馈,然后我可以进一步帮助你。


I tried many type of solution like:

-htmlentities -> When using this, all words have ñ will not display

-htmlspecialchars -> all words have ñ will be "?"

-utf8_encode ->all words have ñ will be "?"

-html_entity_decode(htmlentities($test)); ->When using this, all words have ñ will not display

my code what just a simple select, this is my code:

if (isset($_GET['cityname1'])) 
{

$cityname = strval($_GET['cityname1']);
$cname = mysql_query("SELECT cityname FROM city WHERE provinceid = '$cityname' ORDER BY cityname ASC");

echo "<option value='0'>Select Territory</option>";

  while($provincerow = mysql_fetch_array($cname))
  {
  $pname = htmlentities($provincerow['cityname']);
  echo "<option value='{$pname}'>{pname}</option>";
  }
}
else
{
echo "Please Contact the technical team";
}

解决方案

Ahh, you have stumbled into the wondrous world of character encodings in browsers, PHP and MySQL.

Handling these characters is not something trivial since it is dependent on a number of factors. Normally speaking communication between PHP and MySQL is not in UTF-8 meaning that special characters (like ñ) get jumbled. So setting the connection to UTF-8 is a good start. Furthermore, it can be the case that PHP is not operating in a UTF-8 compliant manner, which can be checked (and set) using the function described here.

When this settings have been set correctely, you should be able to use the html_entities function to properly replace the character to the HTML character encoding (ñ).

The main problem with communcation between different services (like PHP and MySQL) is that when they are not using the same character encoding, characters (which are basically numbers) will be jumbled. Since both MySQL and PHP would be using different numbers for a certain character. For non special characters (like the non-accented alphabet) this works out, since these are extremely standardised, yet for more odd characters there still is some struggle as you have experienced.

Note that this answer assumes a basic setup, if I have made an unjustified assumption, please provide feedback, then I can help you further.

这篇关于如何用ñ显示数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆