0

I am fetching data from a table, through mysqli query. I am able to successfully retrieve the data. But I don't know what is the problem, that when I json_encode() the fetched data. Then it doesn't send back the data, but when I print_r() that fetched data, then it is shown. I am using ajax to send request and in response I can successfully get the print_r() data but not when encoded.

PHP Code

//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;
  }
}

The result I get from 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
    )
Nadeem Ahmad
  • 665
  • 3
  • 17
  • 40

1 Answers1

2

You have special character in your output Aprendiz do Senai - 2� Semestre 20 thats why its breaking. try passing UTF8 charset in db connection.

Saurabh Sharma
  • 430
  • 2
  • 11