Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When I use the json_encode() function, the method return a Json with two time the same value: one with the string key and one with an index. I did not have this problem before.

$req = $bdd->prepare("SELECT mail,description FROM identifiant WHERE mail = :mail AND pass=:pass");
        if ($req->execute(array(
                    'mail' => $_COOKIE['mail'],
                    'pass' => $_COOKIE['pass']))) {
            header('Content-type: application/json');

            return json_encode($req->fetchAll());

The response:

[
   {
      "mail": "[email protected]",
      "0": "[email protected]",
      "description": "a description",
      "1": "a description"
   }
]

How can I do for don't have index keys ?

share|improve this question
    
He did not realize that the SQL query returned an associative array + normal indexed values. Down voting the question for that is rather OTT. –  DroidOS Oct 9 '13 at 9:07
    
try this one : return json_encode($req->fetchAll(PDO::FETCH_ASSOC)); –  ratnesh dwivedi Oct 9 '13 at 9:11

4 Answers 4

up vote 4 down vote accepted

Use PDO::FETCH_ASSOC fetching mode:

return json_encode($req->fetchAll(PDO::FETCH_ASSOC));
share|improve this answer

you have to use PDO::FETCH__ASSOC as a parameter

$req = $bdd->prepare("SELECT mail,description FROM identifiant WHERE mail = :mail AND pass=:pass");
        if ($req->execute(array(
                    'mail' => $_COOKIE['mail'],
                    'pass' => $_COOKIE['pass']))) {
            header('Content-type: application/json');

  return  json_encode($req->fetchAll(PDO::FETCH_ASSOC));

}

share|improve this answer

use this:

PDO::FETCH_ASSOC: returns an array indexed by column name as returned in your result set

PDO::FETCH_BOTH (default): returns an array indexed by both column name and 0-indexed column number as returned in your result set

PDO::FETCH_BOUND: returns TRUE and assigns the values of the columns in your result set to the PHP variables to which they were bound with the PDOStatement::bindColumn() method

PDO::FETCH_CLASS: returns a new instance of the requested class, mapping the columns of the result set to named properties in the class. If fetch_style includes PDO::FETCH_CLASSTYPE (e.g. PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE) then the name of the class is determined from a value of the first column.

PDO::FETCH_INTO: updates an existing instance of the requested class, mapping the columns of the result set to named properties in the class

PDO::FETCH_LAZY: combines PDO::FETCH_BOTH and PDO::FETCH_OBJ, creating the object variable names as they are accessed

PDO::FETCH_NUM: returns an array indexed by column number as returned in your result set, starting at column 0

PDO::FETCH_OBJ: returns an anonymous object with property names that correspond to the column names returned in your result set

PDOStatement::fetch

return json_encode($req->fetchAll(PDO::FETCH_ASSOC));
share|improve this answer

It's not json_encode, it's because your PDO instance's fetch mode is set to PDO::FETCH_BOTH. See the documentation for PDOStatement::fetchAll's fetch style.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.