XML을 가져 와서 일부 데이터를 선택하고이를 데이터베이스 (MySQL)에 삽입하는 PHP 스크립트가 있습니다. 내 모든 필드는 utf8_bin입니다. 이 XML은 ISO-8859-1이며이를 전송하는 다른 사이트이기 때문에이 XML을 변경할 수 없습니다.데이터베이스에 삽입 할 때 문자가 손상됩니다.
예 : 문자열 "NG Contábil"은 "db Contill"로 설정됩니다. 내 스크립트입니다 :
<?php
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
include '/PagSeguroLibrary/PagSeguroLibrary.php';
include '/PagSeguroLibrary/domain/PagSeguroAccountCredentials.class.php';
include 'conexao.php';
include 'alias_array.php';
include 'retorna_cpf.php';
$conexao = ConectaBD::get_instance();
$conexao->conectar_pronto();
$conexao->BD_pronto();
//(...)
$xml = simplexml_load_file('arquivo.xml');
if($xml === null)
$xml = simplexml_load_string($resposta_transacao);
$status = $xml->status;
$nome = $xml->sender->name;
$email = $xml->sender->email;
$codigo = $xml->code;
$vetor = $xml->items->item;
$cpf = retorna_cpf($email);
foreach($vetor as $v)
{
$nome_produto = $v->description;
if($nome_produto != 'frete')
{
//Retorna o id do produto a partir da descrição
$result = mysql_query('SELECT id_product
FROM ps_product_lang
WHERE name = "'.$nome_produto.'"');
$array = Array();
while($row = mysql_fetch_alias_array($result))
{
foreach($row as $linha)
array_push($array, $linha);
}
mysql_query('INSERT INTO pagamento(Status, Nome, Email, CPF, idproduto, Codigo, Inscrito, id, Enviado, NomeProduto)
VALUES ("'.$status.'", "'.$nome.'", "'.$email.'", "'.$cpf.'", "'.$array[0].'", "'.$codigo.'", 0, null, 0, "'.$nome_produto.'")');
}
}
fclose($arquivo);
unlink('arquivo.xml');
?>
모든 답변 주셔서 감사합니다!
XML의 처음 몇 줄을 붙여 넣을 수 있습니까? – Sjoerd
'utf8'또는 'utf-8'입니까? – EmCo
나는 또한 같은 문제가 .. mysql_set_charset() 사용하여 내 문제를 해결 ... http://php.net/manual/en/function.mysql-set-charset.php 모양을 가지고 – alwaysLearn