PHP:SQL请求工作正常但对某些元素返回text = null

时间:2014-05-22 15:28:39

标签: 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":"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表的内容:

enter image description here

以下是表格的结构:

enter image description here

<?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();
}

?>

2 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为法语字母,该函数无法解析它们。 尝试使用标志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;

答案 1 :(得分:1)

同意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');
感谢做这个功能的人,我忘记了从哪里得到它。