PHP:SQL请求工作正常,但某些元素返回text=null
我的PHP脚本工作正常,但对于某些元素,我得到的是空值,而表中的文本如下所示:PHP:SQL请求工作正常,但某些元素返回text=null,php,pdo,character-encoding,json,Php,Pdo,Character Encoding,Json,我的PHP脚本工作正常,但对于某些元素,我得到的是空值,而表中的文本如下所示: [ { "etat":"online", "nb_visiteurs":"0", "id":"1", "date_debut":"19\/05\/2014", "prix":"40", "description":null, "id_author":"1", "titre":null }, {
[
{
"etat":"online",
"nb_visiteurs":"0",
"id":"1",
"date_debut":"19\/05\/2014",
"prix":"40",
"description":null,
"id_author":"1",
"titre":null
},
{
"etat":"online",
"nb_visiteurs":"0",
"id":"2",
"date_debut":"21\/05\/2014",
"prix":"30",
"description":null,
"id_author":"1",
"titre":null
},
{
"etat":"offline",
"nb_visiteurs":"0",
"id":"3",
"date_debut":"22\/05\/2014",
"prix":"23",
"description":"TOP NOIR STYLE ASIATIQUE EN BON ETAT T 3\r\n\r\nFAIRE PROPOSITION",
"id_author":"1",
"titre":"Top noir style asiatique en bon etat t3"
},
{
"etat":"online",
"nb_visiteurs":"0",
"id":"4",
"date_debut":"22\/05\/2014",
"prix":"45",
"description":null,
"id_author":"1",
"titre":"Lit+sommier+matelas+ table de chevet+commode"
}
]
以下是我的annonces表的内容:
下面是表格的结构:
<?php
$PARAM_hote='aaaaaa';
$PARAM_port='3306';
$PARAM_nom_bd='bbbbbbbbb';
$PARAM_utilisateur='ccccccccc';
$PARAM_mot_passe='dddddddddd';
// Create connexion to BDD
$connexion = new PDO('mysql:host='.$PARAM_hote.';port='.$PARAM_port.';dbname='.$PARAM_nom_bd, $PARAM_utilisateur, $PARAM_mot_passe);
try {
// Getting username / password
$username = $_POST['username'];
$password = $_POST['password'];
// Prepare SQL request to check if the user is in BDD
$result1=$connexion->prepare("select * from tbl_user where username = '".$username."' AND password = '".$password. "'");
$result1->execute();
// Getting all results in an array
$arrAllUser = $result1->fetchAll(PDO::FETCH_ASSOC);
// If the size of array is equal to 0, there is no user in BDD
if (sizeof($arrAllUser) == 0) {
echo "fail";
}
// In this case, a user has been found, create a JSON object with the user information
else {
// Getting id of the user
$id_user = $arrAllUser[0]['id'];
// Prepare a second SQL request to get all annonces posted by the user
$result2=$connexion->prepare(" select * from annonces where id_author = '".$id_user."' ");
$result2->execute();
// Getting all annonces posted by the user in an array
$arrAllAnnonces = $result2->fetchAll(PDO::FETCH_ASSOC);
// Set in key user the USER structure
$array['user'] = $arrAllUser[0];
// Set in key annonces all annonces from the user
$array['annonces'] = $arrAllAnnonces;
// JSON encore the array
$json_result = json_encode($array);
echo $json_result;
}
} catch(Exception $e) {
echo 'Erreur : '.$e->getMessage().'<br />';
echo 'N° : '.$e->getCode();
}
?>
之所以发生这种情况,是因为函数无法解析法语字母。 尝试使用标志JSON_UNESCAPED_UNICODE
json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE)
你可以玩旗子
试试这个
$output = array();
foreach ($array as $row)
{
$output[]=array_map("utf8_encode", $row);
}
$json_result = json_encode($output);
echo $json_result;
同意volkinc的回答,如果您有一个新的php版本可以使用 json_unescaped_unicode而不是json_encode 如果您的php版本5.3及更高版本的unescaped无法工作,只需在脚本底部添加此函数,并使用它而不是json_encode
function my_json_encode($in) {
$_escape = function ($str) {
return addcslashes($str, "\v\t\n\r\f\"\\/");
};
$out = "";
if (is_object($in)) {
$class_vars = get_object_vars(($in));
$arr = array();
foreach ($class_vars as $key => $val) {
$arr[$key] = "\"{$_escape($key)}\":\"{$val}\"";
}
$val = implode(',', $arr);
$out .= "{{$val}}";
}elseif (is_array($in)) {
$obj = false;
$arr = array();
foreach($in AS $key => $val) {
if(!is_numeric($key)) {
$obj = true;
}
$arr[$key] = my_json_encode($val);
}
if($obj) {
foreach($arr AS $key => $val) {
$arr[$key] = "\"{$_escape($key)}\":{$val}";
}
$val = implode(',', $arr);
$out .= "{{$val}}";
}else {
$val = implode(',', $arr);
$out .= "[{$val}]";
}
}elseif (is_bool($in)) {
$out .= $in ? 'true' : 'false';
}elseif (is_null($in)) {
$out .= 'null';
}elseif (is_string($in)) {
$out .= "\"{$_escape($in)}\"";
}else {
$out .= $in;
}
return "{$out}";
}
使用my_json_encode而不是json_encode,不要忘记这一行:
header('content-type:text/html;charset=utf-8');
感谢制作此函数的人,我忘记了从何处获得此函数。此标志的结果相同。。你有其他想法吗?只是为了测试目的,用法语将字符串改为“英语中的某些文本”,以检查法语字母是否是原因,如果我用英语文本替换法语文本,我的脚本工作正常。这样更好!现在我得到了所有文本,但在文本中我得到了“\u00e9”。