简体   繁体   中英

json_encode is truncating the object data read from mysql database

I am developing a web application in PHP 5.6.8 with MySql as database engine.

In my method to authenticate the User for appropriate credentials i am making a call to the database and retrieved the user object. The function code is :

function getBoosterDetails($patientId){
       $dbConnection = new Database();

       $sql = "SELECT * from users where id = :patientId";
       try {
           $db = $dbConnection->getConnection();
           $stmt = $db->prepare($sql);
           $stmt->bindParam("patientId", $patientId);
           $stmt->execute();
           $userDetails = $stmt->fetchAll(PDO::FETCH_OBJ);
           $db = null;
           $_SESSION['userDetails'] = $userDetails;

           echo '{"user": ' . json_encode($userDetails) . '}';


       } catch(PDOException $e) {
           echo '{"error":{"text":'. $e->getMessage() .'}}'; 
       } catch(Exception $e1) {
           echo '{"error11":{"text11":'. $e1->getMessage() .'}}'; 
       } }

For any valid input I have been getting the error that object is not valid. I tried Inspecting the userDetails object in Advanced Rest Client Chrome Plugin. What I observe is that while the var dump of the object has teh whole object data the echo of json_encode has truncation as displayed in the below image.

![enter image description here][1]

I have setup the Apache & MySql Servers with XAMPP (and also tried with MAMP earlier and met with the same result.)

// this is the var_dump
    stdClass object
(
    [username]  => pavan
    [rolename]  => Patient
    [userid]  => 5
)

//this is the json_encode echo output
{"user": [{"username":"pavan", "rolename":"Patient", "userid":

The content is getting truncated when it is json encoded Could you please help me out resolving this.

Don't wrap json-in-json. That's just wrong.

Build one SINGLE data structure in your host language (eg php), then encode the ENTIRE structure:

$data = array(
   'user' => $userDetails
);
$json = json_encode($data);

Your code is producing syntactically ILLEGAL json...

   echo '{"user":' . json_encode($userDetails) . '}}';**
         ^--open #1                               ^--close #1
                                                   ^--close #2

What is this #2 brace closing? If you done the single-structure encode, this would have been impossible.

can you try below code instead of existing one?

We just changed PDO::FETCH_OBJ to PDO::FETCH_ASSOC, it returns array format.

Updated code:

function getBoosterDetails($patientId){ $dbConnection = new Database();

   $sql = "SELECT * from users where id = :patientId";
   try {
       $db = $dbConnection->getConnection();
       $stmt = $db->prepare($sql);
       $stmt->bindParam("patientId", $patientId);
       $stmt->execute();
       $userDetails = $stmt->fetchAll(PDO::FETCH_ASSOC);
       $db = null;
       $_SESSION['userDetails'] = $userDetails;

       echo '{"user": ' . json_encode($userDetails) . '}';


   } catch(PDOException $e) {
       echo '{"error":{"text":'. $e->getMessage() .'}}'; 
   } catch(Exception $e1) {
       echo '{"error11":{"text11":'. $e1->getMessage() .'}}'; 
   } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM