“ JSON_ENCODE()”不适用于获取的数据-PHP

时间:2019-01-20 14:29:17

标签: php mysqli

我正在通过mysqli查询从表中获取数据。我能够成功检索数据。但是我不知道这是什么问题,当我json_encode()提取数据时。然后它不会发送回数据,但是当我print_r()提取数据时,它就会显示出来。我正在使用ajax发送请求,作为响应,我可以成功获取print_r()数据,但编码时无法获取。

PHP代码

//The Actuall query
$sql = "SELECT `companies_jobs`.*, `companies`.`name`, `companies`.`location`, `companies`.`image` FROM `companies`, `companies_jobs` WHERE `companies_jobs`.`comp_fk` = `companies`.`id`";

show($sql, $db_conn);


public
function show($sql, $db_conn) {
  $data = array();
  if ($db_conn - > query($sql)) {
    $r = $db_conn - > query($sql);
    if ($r - > num_rows > 0) {
      for ($i = 0; $i < $r - > num_rows; $i++) {
        $data[] = $r - > fetch_assoc();
      }
      //echo json_encode($data);
      print_r($data);
    }
  } else {
    echo $db_conn - > error;
  }
}

我从print_r()获得的结果

Array
  (
    [0] => Array(
      [id] => 1[job_title] => Assistant documentation technique(h / f)[job_link] => https: //career5.successfactors.eu/career?career%5fns=job%5flisting&company=LiMySLive&navBarLevel=JOB%5fSEARCH&rcm%5fsite%5flocale=en%5fUS&career_job_req_id=13964&selected_lang=fr_FR&jobAlertController_jobAlertId=&jobAlertController_jobAlertName=&_s.crb=Zq7lxyNNjW%2ferSKnwNojVIrWrGQ%3d
      [job_date] => Posted on 01 / 19 / 2019[job_level] => Young Professionals[job_category] => Customer Service[job_function] => [job_loc] => France(FR)[job_timing] => full time[job_company] => [job_working_hours] => [job_salary] => [time_stamp] => {
        "stamp": 1547935242725,
        "text": "03:00:42 AM"
      }
      [date] => {
        "date": "20/1/2019",
        "dateText": "Jan 20, 2019"
      }
      [comp_fk] => 1[name] => Libherr[location] => Germany[image] => http: //www.fundacionfin.es/wp-content/uploads/cache/images/2018/08/liebherr/liebherr-2580993250.jpg
    )

    [1] => Array(
      [id] => 2[job_title] => Praktikant(m / w / d) im Bereich Personalentwicklung[job_link] => https: //career5.successfactors.eu/career?career%5fns=job%5flisting&company=LiMySLive&navBarLevel=JOB%5fSEARCH&rcm%5fsite%5flocale=en%5fUS&career_job_req_id=13923&selected_lang=de_DE&jobAlertController_jobAlertId=&jobAlertController_jobAlertName=&_s.crb=Zq7lxyNNjW%2ferSKnwNojVIrWrGQ%3d
      [job_date] => Posted on 01 / 19 / 2019[job_level] => Students[job_category] => Human resources[job_function] => [job_loc] => Germany(DE)[job_timing] => full time[job_company] => [job_working_hours] => [job_salary] => [time_stamp] => {
        "stamp": 1547935242725,
        "text": "03:00:42 AM"
      }
      [date] => {
        "date": "20/1/2019",
        "dateText": "Jan 20, 2019"
      }
      [comp_fk] => 1[name] => Libherr[location] => Germany[image] => http: //www.fundacionfin.es/wp-content/uploads/cache/images/2018/08/liebherr/liebherr-2580993250.jpg
    )

    [2] => Array(
      [id] => 3[job_title] => Senior Strategic Buyer[job_link] => https: //career5.successfactors.eu/career?career%5fns=job%5flisting&company=LiMySLive&navBarLevel=JOB%5fSEARCH&rcm%5fsite%5flocale=en%5fUS&career_job_req_id=13943&selected_lang=en_US&jobAlertController_jobAlertId=&jobAlertController_jobAlertName=&_s.crb=Zq7lxyNNjW%2ferSKnwNojVIrWrGQ%3d
      [job_date] => Posted on 01 / 18 / 2019[job_level] => Professionals[job_category] => Purchasing[job_function] => [job_loc] => United States(US)[job_timing] => full time[job_company] => [job_working_hours] => [job_salary] => [time_stamp] => {
        "stamp": 1547935242725,
        "text": "03:00:42 AM"
      }
      [date] => {
        "date": "20/1/2019",
        "dateText": "Jan 20, 2019"
      }
      [comp_fk] => 1[name] => Libherr[location] => Germany[image] => http: //www.fundacionfin.es/wp-content/uploads/cache/images/2018/08/liebherr/liebherr-2580993250.jpg
    )

    [3] => Array(
      [id] => 4[job_title] => Aprendiz do Senai - 2� Semestre 2019[job_link] => https: //career5.successfactors.eu/career?career%5fns=job%5flisting&company=LiMySLive&navBarLevel=JOB%5fSEARCH&rcm%5fsite%5flocale=en%5fUS&career_job_req_id=13942&selected_lang=pt_BR&jobAlertController_jobAlertId=&jobAlertController_jobAlertName=&_s.crb=Zq7lxyNNjW%2ferSKnwNojVIrWrGQ%3d
        [job_date] => Posted on 01 / 18 / 2019[job_level] => Pupils[job_category] => Production[job_function] => [job_loc] => Brazil(BR)[job_timing] => full time[job_company] => [job_working_hours] => [job_salary] => [time_stamp] => {
          "stamp": 1547935242725,
          "text": "03:00:42 AM"
        }
        [date] => {
          "date": "20/1/2019",
          "dateText": "Jan 20, 2019"
        }
        [comp_fk] => 1[name] => Libherr[location] => Germany[image] => http: //www.fundacionfin.es/wp-content/uploads/cache/images/2018/08/liebherr/liebherr-2580993250.jpg
    )

1 个答案:

答案 0 :(得分:2)

输出Aprendiz do Senai - 2� Semestre 20中有特殊字符,这就是其中断的原因。尝试在数据库连接中传递UTF8字符集。