简体   繁体   English

插入数据库的值显示奇怪的字符

[英]Values inserted into database show weird characters

I am taking data in an xml feed from readability and inserting it into a database and then outputting it. 我从可读性的xml提要中获取数据,并将其插入数据库中,然后输出。 The charset of the xml feed is UTF-8 , my html page headers are also UTF-8 . xml提要的字符集是UTF-8 ,我的html页面标题也是UTF-8 I even saved the code through my text editor as UTF-8 and also my DB is set to utf8_unicode_ci . 我什至通过我的文本编辑器将代码保存为UTF-8 ,并且我的数据库设置为utf8_unicode_ci I can't figure out why this is happening. 我不知道为什么会这样。

Code: 码:

$xml = simplexml_load_file( "http://readability.com/christopherburton/latest/feed" );
$json = json_encode( $xml );
$array = json_decode( $json,TRUE );
$items = $array['channel']['item'];

$DB = new mysqli('localhost', 'secret', 'secret', 'secret' );
if( $DB->connect_errno ){
    print "failed to connect to DB: {$DB->connect_error}";
    exit( 1 );
}
$match = "#^(?:[^\?]*\?url=)(https?://)(?:m(?:obile)?\.)?(.*)$#ui";
$replace = '$1$2';

foreach( $items as $item ){
    $title = $item['title'];
    $url = preg_replace( $match,$replace,$item['link'] );
    $title_url[] = array( $title,$url );
    $sql_values[] = "('{$DB->real_escape_string( $title )}','{$DB->real_escape_string( $url )}')";
}
$SQL = "INSERT IGNORE INTO `read`(`title`,`url`) VALUES\n ".implode( "\n,",array_reverse( $sql_values ) );
if( $DB->query( $SQL ) ){
} else {
    print "failed to INSERT: [{$DB->errno}] {$DB->error}";
}
$DB->set_charset('utf8');

Your problem is the place where you put $DB->set_charset('utf8'); 您的问题是放置$DB->set_charset('utf8');

You need to tell the database in which charset you send or want to receive the data before you do the query. 执行查询之前,您需要告诉数据库要在哪个字符集上发送或要在其中接收数据。

But because you have $DB->set_charset('utf8'); 但是因为您有$DB->set_charset('utf8'); after your queries the command has no effect, to the previous queries. 查询后,该命令无效,对先前的查询无效。

If no charset is defined for the connection then the DMBS uses the charset that is set as default in the configs. 如果未为连接定义任何字符集,则DMBS将使用在配置中默认设置的字符集。 For mysql this this may be eg latin1 . 对于mysql,这可能是例如latin1 Because of that mysql thinks it received data that is encoded in eg latin1 and would convert it to utf8 thats why you see these strange symbols. 因此,mysql认为它接收到以latin1编码的数据, 并将转换utf8这就是为什么您看到这些奇怪的符号的原因。

To solve the problem you just need ensure that $DB->set_charset('utf8'); 要解决该问题,您只需要确保$DB->set_charset('utf8'); is called before the queries that passes or wants to receive data in utf8 . utf8中传递或希望接收数据的查询之前调用。

For your example you could place it right after the if( $DB->connect_errno ){} because at that place the connection was successfully established. 对于您的示例,您可以将其放置在if( $DB->connect_errno ){}因为在该位置成功建立了连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM