我有一个名为users的数据库表,其中一个用户的姓氏包含“ñ” 当我从users表中查询时,它为包含“ñ”的姓氏返回null,但对于其他人来说,它就没事了。
答案 0 :(得分:2)
根据json_encode的文档:
此功能仅适用于UTF-8编码数据。
如果你的数据在latin1_swedish_ci中,它将不接受ñ。我的建议是更改您的数据库并在任何地方使用utf-8(这对我很有用),或者尝试使用多字节函数将输入数据更改为utf-8。这样的事情应该有效:
<?php
$input = mb_convert_encoding( $yourstring, 'UTF-8', mb_detect_encoding( $yourstring ) );
再次;我认为使用UTF-8来实现一切都是更明智的举措,可以帮助您避免以后的麻烦。
答案 1 :(得分:1)
我无法用
重现错误<?php
ini_set('default_mimetype', 'text/html');
ini_set('default_charset', 'utf-8');
echo 'phpversion: ', phpversion(), "<br />\n";
$mysqli = connect();
echo 'mysqli server version: ', mysqli_get_server_version($mysqli), "<br />\n";
echo 'mysqli client version: ', mysqli_get_client_version($mysqli), "<br />\n";
echo 'mysqli proto info: ', mysqli_get_proto_info($mysqli), "<br />\n";
echo '<pre>mysqli charset: '; var_dump(mysqli_get_charset($mysqli)); echo "</pre>\n";
setup($mysqli);
$result = mysqli_query($mysqli, 'SELECT x FROM foo') or die(__LINE__.mysqli_error($mysqli));
while ( $row=mysqli_fetch_assoc($result) ) {
echo '<pre>', $row['x'], "<pre>\n";
}
function connect() {
$mysqli = mysqli_connect('localhost', 'localonly', 'localonly', 'test') or die(__LINE__.mysqli_connect_error());
mysqli_set_charset($mysqli, 'utf8') or die(__LINE__.mysqli_error($mysqli));
return $mysqli;
}
function setup($mysqli) {
mysqli_query($mysqli, 'CREATE TEMPORARY TABLE foo (id int auto_increment, x varchar(32), primary key(id))') or die(__LINE__.mysqli_error($mysqli));
$v = chr(0xC3). chr(0x91);
mysqli_query($mysqli, "INSERT INTO foo (x) VALUES ('abc'),('$v')") or die(__LINE__.mysqli_error($mysqli));
}
在我的电脑上打印
phpversion: 5.3.5
mysqli server version: 50508
mysqli client version: 50007
mysqli proto info: 10
mysqli charset:
object(stdClass)[2]
public 'charset' => string 'utf8' (length=4)
public 'collation' => string 'utf8_general_ci' (length=15)
public 'dir' => string '' (length=0)
public 'min_length' => int 1
public 'max_length' => int 3
public 'number' => int 33
public 'state' => int 1
public 'comment' => string 'UTF-8 Unicode' (length=13)
abc
Ñ