0

could you explain me what I'm doing wrong, c/z I'm totally confused with what I saw

Let's say we have XML which contains a node like that

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

if I do

$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);

I see in error.log

[Mon Oct 06 23:34:21.018149 2014] [:error] [pid 13255] [client 127.0.0.1:44050] Calling stored procedure: call 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', @retcode);

But if I run

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

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

I'm able to see a normal cyrillic result in my terminal,

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

So what would you recommend to achieve my goal to put into a DB a proper cyrillic string?

Philipp Grigoryev
  • 1,985
  • 3
  • 17
  • 23

1 Answers1

0

Assuming that your database tables and PHP files are UTF-8 encoded, after the database connection, send those queries:

  • SET NAMES utf8
  • SET CHARSET utf8

For instance, if you're using PDO, you would do something like this:

$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');

If your database tables aren't UTF-8, change it accordingly.

Community
  • 1
  • 1
Pedro Amaral Couto
  • 2,056
  • 1
  • 13
  • 15