PHP在不同的上下文中有不同的error_log和echo结果

时间:2014-10-07 04:58:07

标签: php string utf-8

你可以解释一下我做错了什么,c / z我对我所看到的完全感到困惑

假设我们的XML包含类似

的节点
<Name>&#1043;&#1040;&#1041;&#1048;&#1044;&#1059;&#1051;&#1051;&#1048;&#1053; &#1040; &#1047;</Name>

如果我这样做

$lastname = $xpath->query(//Name/text()")->item(0)->textContent;

$sql = "call save_ticket_ordered($order_id, $ticket_num, '$lastname', '$time', '$user', @retcode);";

error_log("Calling stored procedure: " . $sql);

我在error.log中看到了

[Mon Oct 06 23:34:21.018149 2014] [:error] [pid 13255] [client 127.0.0.1:44050]调用存储过程:调用save_ticket_ordered(103696202,78290448213275,'\ xd0 \ x93 \ xd0 \ x90) \ xd0 \ x91 \ xd0 \ x98 \ xd0 \ x94 \ xd0 \ xa3 \ xd0 \ x9b \ xd0 \ x9b \ xd0 \ x98 \ xd0 \ x9d \ xd0 \ x90 \ xd0 \ x97','03 .10.2014 09:53' ,'xxxxxxx',@ verycode);

但如果我跑

$xmldoc = new DOMDocument();
$xmldoc->load("response.xml");

$xpath = new DOMXPath($xmldoc);
$lastname = $xpath->query("//Name/text()")->item(0)->textContent;
echo $lastname;

我能够在终端上看到正常的西里尔文结果,

ГАБИДУЛЛИНАЗ

那么你会建议我把目标放在DB中一个合适的西里尔字符串?

1 个答案:

答案 0 :(得分:0)

假设您的数据库表和PHP文件是UTF-8编码的,在数据​​库连接之后,发送这些查询:

  • SET NAMES utf8
  • SET CHARSET utf8

例如,如果你正在使用PDO,你会做这样的事情:

$this->pdo = new \PDO($dsn, $user, $pass,
                             array(
                                 \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
                                 \PDO::ATTR_EMULATE_PREPARES   => false,
                             ));
$this->pdo->exec('SET NAMES utf8');
$this->pdo->exec('SET CHARSET utf8');

如果您的database tables aren't UTF-8, change it accordingly