0

my php code generate a non-Valid json output error

my php code :

$questions = array();
while($question = mysql_fetch_array($result, MYSQL_ASSOC)) {
$questions[] = array('question'=> $question);
}
print_r ($questions);

$newQuestions = array('questions' => array());

foreach($questions as $key => $question){
    $newQuestion = array(
            'question' => $question['question']['question'],
            'correct' => $question['question']['correct'],
            'answers' => array(
                    $question['question']['answer1'],
                    $question['question']['answer2'],
                    $question['question']['answer3'],
                    $question['question']['answer4']
                )
    );

    $newQuestions['questions'][] = $newQuestion;

}

$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);

echo '<br/><br/>';
echo $output;

table fields :

Question :
correct  :
answer 1 :
answer 2 :
answer 3 :
answer 4 :

Example :

Question : is php a good language ?
correct  : 1
answer 1 : yes
answer 2 : no
answer 3 : maybe
answer 4 : good

the output is OK, and formated as I want.

output sample : http://pastebin.com/eefS7KYW

I am sure my php code is correct but I don't know where is exactly the issue !!

============== Fixed : it was just two echo $output !

11
  • 1
    json_encode works fine. The issue must be in your code, but I cannot see what exactly from handpainted screenshots. It would seem you're outputting the result twice. Commented Sep 4, 2014 at 23:17
  • looks like its missing a comma Commented Sep 4, 2014 at 23:20
  • @RicardoE as questions is the only key in the root object it's more likely a double print, otherwise it wouldn't appear right after the 'exception' line. Commented Sep 4, 2014 at 23:27
  • @RicardoE json output is missed up in one place only but the whole output is correct and formated properly . Commented Sep 4, 2014 at 23:31
  • Just FYI there are other issues with your code. We are long past using mysql_ functions, use mysqli or pdo instead. You also use variables needlessly. Commented Sep 4, 2014 at 23:32

1 Answer 1

1

Seems like you are doing a lot of variable passing which gets very confusing, very quickly. Especially when all the variables are iterations of 'question'. It appears that you are creating an array from information pulled from a database in the format [questions[question,correct,answers[1,2,3,4]]] This code format may work better?

   $newQuestions = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $newQuestions['questions'][] = array(
    'question'  =>  $row['question'],
    'correct'   =>  $row['correct'],
    'answers'   =>  array(
        $row['answer1'],
        $row['answer2'],
        $row['answer3'],
        $row['answer4']
        )
    );
}

    $output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);

    echo '<br/><br/>';
    echo $output;

Had a semicolon in the wrong place.Fixed the code above and test the code with the following:

<?php

$array = array(
    array('question'=>'Question1',
        'correct'=>3,
        'answer1' => 'Q1Answer1',
        'answer2' => 'Q1Answer2',
        'answer3' => 'Q1Answer3',
        'answer4' => 'Q1Answer4'
    ),
    array('question'=>'Question2',
        'correct'=>3,
        'answer1' => 'Q2Answer1',
        'answer2' => 'Q2Answer2',
        'answer3' => 'Q2Answer3',
        'answer4' => 'Q1Answer4'
    ),
    array('question'=>'Question3',
        'correct'=>3,
        'answer1' => 'Q3Answer1',
        'answer2' => 'Q3Answer2',
        'answer3' => 'Q3Answer3',
        'answer4' => 'Q1Answer4'
    )
);

$newQuestions = array();
//while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($array as $row){
    $newQuestions['questions'][] = array(
    'question'  =>  $row['question'],
    'correct'   =>  $row['correct'],
    'answers'   =>  array(
        $row['answer1'],
        $row['answer2'],
        $row['answer3'],
        $row['answer4']
        )
    );
}

print_r($newQuestions);
$output = json_encode(($newQuestions),JSON_UNESCAPED_UNICODE);

echo '<br/><br/>';
echo $output;

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Had a semicolon in the wrong spot. Tested and fixed.
it works ! thank you ! it was just twice echo that print 2 output :D

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.