dongping5230 2012-12-27 03:39
浏览 62
已采纳

PHP 5.4.9 MySQL 5.5.28.3 - 选择带有UniCode字符的字段将返回空白结果

I've been searching the web for hours and nothing I find seems to help. This was working fine when I was on PHP 5.3 and MySQL 5.1. When I upgraded both of these, I realized that selects done against fields with UniCode characters in them give blank results.

The table is set to UTF8 and the field in particular has also been set to UTF8 just in case. I have a charset=utf-8 in the header area of my document.

I made two test posts in my forum, one with straight text and one with a curly quote in it. I then created a simple PHP query to fetch the two rows. The gist of the code is this:

<?php

$con = mysql_connect("XXXXX","XXXXX","XXXXX");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("XXXXX", $con);


echo "<br />";
echo "<br />";
echo "PHP 5.4.9 having issues displaying UniCode characters. MySQL 5.5.28.3. FastCGI has been removed. IIS.";
echo "<br />";
echo "<br />";


$result1 = mysql_query("SET NAMES 'utf8'");


$result = mysql_query("SELECT post_body from ubbt_posts where post_id = 358826");

while($row = mysql_fetch_array($result))
  {
  echo "Post Body without unicode: " . $row['post_body'];
  echo "<br />";
  echo "<br />";
  }


$result = mysql_query("SELECT post_body from ubbt_posts where post_id = 358825");

while($row = mysql_fetch_array($result))
  {
  echo "Post Body with unicode: " . $row['post_body'];
  echo "<br />";
  echo "<br />";
  }

mysql_close($con);

?>

The behavior is that the non-unicode post displays properly. The other one doesn't display at all. It's not that it displays a square or block instead of the quote - it doesn't show anything.

I've tried changing the mysql_fetch_array to mysql_fetch_assoc. That didn't help. I tried utf8_encode out of frustration and that didn't help either.

I'd read that some people had issues with the combo of IIS / PHP 5.4.9 / FastCGI so I deinstalled FastCGI. At least I tried my best to - the files are all gone and IIS is pointing again to the php-cgi.exe file. But PHP still reports it's using FastCGI. I think it must be seeing something in the registry? But in any case the files are gone so it really can't be. And it didn't help.

I'll try even wild ideas at this point to get it working. Any thoughts?

UPDATES:

I have now tested this with both PHP and ASP. Both languages give the identical same behavior. My result set with updated code gives these results:

Post Subject without unicode: test post without unicode

Post Body without unicode: test post with just normal characters in it, as well as xyzzy for testing.

Length of body field: 74

Post Subject with unicode: test post with unicode

Post Body with unicode:

Length of body field: 0

So with the length being checked, both ASP and PHP report the length of that field is zero.

Could it be ODBC that is having an issue? Could it be FastCGI still interfering even though I tried to remove it?

UPDATE 2 -

What I had to do to fix this is to reload the data in from a backup, into the table already set as UTF8 and the field set to UTF8. Then I had to do the set names utf8 command in the code right after the connect. I don't think FastCGI was involved here - when I reinstalled FastCGI things were still OK. I still have some niggling issues remaining but I think the table set and reload handled most of my issues.

Lisa

  • 写回答

1条回答 默认 最新

  • duanao2585 2012-12-27 03:43
    关注

    Try setting utf8 in another way -

    mysql_set_charset('utf8', $con);
    

    In place of this --

    mysql_query("SET NAMES 'utf8'");
    

    Where $con is a connection created with mysql_connect() method

    NOTE - Try to use PDO or mysqli

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决