0

I'm trying to encode data coming from Mysql database into JSON format via PHP.Here's the code segment:

try
{
    $statement = $db->prepare($query);
    $result = $statement->execute($query_params);

    $rows = $statement->fetchAll();

    echo print_r($rows)."<br/>";

    foreach($rows as $row)
    {
        foreach($row as $a)
    {
        $studentInfo = array();
        $studentInfo["course"] = $a["coursename"];
        $studentInfo["grade"] = $a["grade"];
        array_push($response['info'], $studentInfo);
    }
    }
    echo json_encode($response);
}

But I'm not able to encode this array into JSON. The print_r($rows) prints the following:

Array (
    [0] => Array (
        [id] => 22222
        [coursename] => sp1
        [grade] => B
    )
    [1] => Array (
        [id] => 22222
        [coursename] => sw1
        [grade] => A-
    )
    [2] => Array (
        [id] => 22222
        [coursename] => sw2
        [grade] => B+
    )
)
1

And echo json_encode($response) prints the following:

{"info":null}

Could anyone please show me how to parse this array into JSON? I would like to get to each course and its grade in JSON format.

3
  • 1
    Remove the inner loop. $row has the contents you are looking for instead of $a Commented Feb 25, 2014 at 1:00
  • 2
    or just json_encode(array('info'=>$rows)) from your original $rows ... no need for any of the loops.. Commented Feb 25, 2014 at 1:08
  • Thanks datasage. Removing the inner loop solved the porblem!!! Commented Feb 26, 2014 at 16:00

3 Answers 3

2
try
{
    $statement = $db->prepare($query);
    $result = $statement->execute($query_params);

    $rows = $statement->fetchAll();

    echo print_r($rows)."<br/>";

    $response=array();
    $info=array();

    foreach($rows as $row)
    {      
        $studentInfo = array();
        $studentInfo["id"]=$row["id"];
        $studentInfo["course"] = $row["coursename"];
        $studentInfo["grade"] = $row["grade"];
        array_push($info, $studentInfo);    
    }

    $response['info']=$info;
    echo json_encode($response); // or echo json_encode($info);// whatever you want
 }
Sign up to request clarification or add additional context in comments.

Comments

1

I'd do something like this:

try {
    $statement = $db->prepare($query);
    $result = $statement->execute($query_params);
    if($result) {
        $arr = Array();
        while ($row = $result->fetch_assoc()) {
            $arr[] = $row;
        }
     echo json_encode($arr);
    }
}

Comments

0

json_encode() can directly encode the array.

at your code line 8, can do this

echo json_encode($rows);

Please click here to refer for more info http://php.net/manual/en/function.json-encode.php

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

Output:
{"a":1,"b":2,"c":3,"d":4,"e":5}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.