0

i am executing multiple "select" statements in my php script. Then I am adding them to array by $Output=array($out, $out1); and then encoding it by json_encode()

the output is coming in format [ [{},{}] , [{}, {}] ]

how do i convert it in form - {"str1": [{},{}], "str2": [{},{}]}

php script:

<?php

require 'DbConnect.php';


$query = ("SELECT * FROM `cars`");
$query1 = ("SELECT * FROM `City`");

if ($query_run = mysql_query($query)){

while ($query_row = mysql_fetch_assoc($query_run)){

    $out [] = $query_row;

}

}else{
    echo 'Fail';
} 

//second query 

if ($query_run1 = mysql_query($query1)){

while ($query_row1 = mysql_fetch_assoc($query_run1)){

$out1 [] = $query_row1;

}

}else{
    echo 'Fail';
} 

$Output=array($out, $out1); 
echo(json_encode($Output));
?>
3
  • ok, so plz tel me how should i encode it? Commented Jul 29, 2013 at 9:56
  • [ [{},{}] , [{}, {}] ] is ur output ...but before encoding put it in array Commented Jul 29, 2013 at 9:58
  • Hi @Pooja my code is not useful for you... Commented Jul 29, 2013 at 10:48

4 Answers 4

1

if you are getting result in [ [{},{}] , [{}, {}] ]

then try

$Output=array('str1'=>$out,'str2'=>$out1); 
  echo(json_encode($Output,JSON_FORCE_OBJECT));
Sign up to request clarification or add additional context in comments.

Comments

0

try this

    $query = "SELECT 
    post_title, 
    guid 
    FROM testwp_posts
    WHERE 
    post_author = $user_id 
    AND
    post_status = 'publish' 
    ORDER BY ID DESC LIMIT $number_of_posts";

        $result = mysql_query($query) or die('Errant query:  '.$query);

        /* create one master array of the records */
        $posts = array();
        if(mysql_num_rows($result)) {
            while($post = mysql_fetch_assoc($result)) {
                $posts[] = array('post'=>$post);
            }
        }


            header('Content-type: application/json');
            echo json_encode(array('posts'=>$posts));

1 Comment

wat is $link in your answer?
0
<?php

require 'DbConnect.php';


$query = ("SELECT * FROM `cars`");
$query1 = ("SELECT * FROM `City`");

if ($query_run = mysql_query($query))
{
    while ($query_row = mysql_fetch_assoc($query_run))
    {   
        $output['cars'][] = $query_row;
    }

}
else
{
    echo 'Fail';
} 

//second query 

if ($query_run1 = mysql_query($query1))
{
    while ($query_row1 = mysql_fetch_assoc($query_run1))
    {
        $output['city'][] = $query_row1;
    }
}
else
{
    echo 'Fail';
} 

echo(json_encode($output));
?>

may it helps

Comments

0

I am not sure if this helps your requirement, but try below things..

Instead of using

$out [] = $query_row;

Use

$out['str1'][] = $query_row;

AND Instead of

$out1 [] = $query_row1;

Use

$out['str2'][] = $query_row1;

And now try this

echo(json_encode($out));

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.