Json not value but print_r is php pdo jquery ajax

if (isset($_POST['continentid'])) {
      $stmt = $dbh->prepare("SELECT * FROM country_tbl WHERE parent_id = ? ");
      $stmt->bindValue(1, $_POST['continentid'], PDO::PARAM_STR);
    if ($stmt->execute()) {
        if ($stmt->rowCount() > 0) {
            while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
            } 
            //print_r($country);
            echo json_encode($country);
            //echo "312321321321";
            //return $country;

        }
    }
}


$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
       for(var i = 0; i < data.length; i++) {
           console.log("PAIR " + i + ": " + data[i].sysid);
           console.log("PAIR " + i + ": " + data[i].name);
       }
    }
});

      

I have this code above which sends a request using jquery ajax, it will return an id to be used as a parameter for a select statement. Then I use those values ​​in the select box. I posted a question for this here . It works fine, now it's weird that the first continent value doesn't give the list of countries if json is used, but when I use print_r it gives me the list of countries, but for another continent the json value is ok, I get the value for the country. The question is why the first value in the list doesn't give a json value, but if print_r does matter, what is wrong with this setting?

Update

If i do

print_r ($ country); echo json_encode ($ country);

for the first element

Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)

      

If i do

print_r ($ country); echo json_encode ($ country);

for the second element

Array
(
    [0] => Array
        (
            [sysid] => 1
            [code] => 140101000
            [name] => China
            [parentid] => 1
        )

    [1] => Array
        (
            [sysid] => 2
            [code] => 140102000
            [name] => Japan
            [parentid] => 1
        )

    [2] => Array
        (
            [sysid] => 3
            [code] => 140103000
            [name] => Hongkong
            [parentid] => 1
        )
)


[
{"sysid":"1","code":"140101000","name":"China","parentid":"1"},{"sysid":"2","code":"140102000","name":"Japan","parentid":"1"},{"sysid":"3","code":"140103000","name":"Hongkong","parentid":"1"}
]

      

Update

I think I found the problem, although I havent found a solution, but I think that the character Γ±

and '

is the reason that they do not return the value to json any ideas on how to get it to return these values?

0


source to share


4 answers


Adding this line $dbh->query("SET CHARACTER SET utf8");

to my config file fixes the problem, so it does have something to do with special characters like Γ±

json_encode with a return value of null if there are special characters



0


source


try it. JSON.parse(data)

convert string to json.



    $.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + data[key].sysid);
           console.log("PAIR " + i + ": " + data[key].name);
       }
    }
});

      

0


source


Try it,

success: function(data) {
   $(data).each(function(i,v){
       console.log("PAIR " + i + ": " + v.sysid);
       console.log("PAIR " + i + ": " + v.name);
   });
}

      

var data = [{
  "sysid": "1",
  "code": "140101000",
  "name": "China",
  "parentid": "1"
}, {
  "sysid": "2",
  "code": "140102000",
  "name": "Japan",
  "parentid": "1"
}, {
  "sysid": "3",
  "code": "140103000",
  "name": "Hongkong",
  "parentid": "1"
}];
$(data).each(function(i, v) {
  console.log("PAIR " + i + ": " + v.sysid);
  console.log("PAIR " + i + ": " + v.name);
});
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      

Run codeHide result


Change your php code snippet like

if ($stmt->rowCount() > 0) {
     $country=array();// initialise $country first
     while ($selected_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
         $country[] = array('sysid' => $selected_row['sys_id'],'name' => $selected_row['countryname']);
     } 
     echo json_encode($country);
 }

      

0


source


$.ajax({
    type: 'POST',
    url: '../include/country.php',
    dataType : "json",
    data: {
        continentid: id
    },
    success: function(data) {
  data=  JSON.parse(data);
      data.each(key,value)  {

           console.log("PAIR " + i + ": " + value.sysid);
           console.log("PAIR " + i + ": " + value.name);
       }
    }
});

      

0


source







All Articles