1

I am trying to encode all the rows of data i get from the DB into JSON and then process it at the client side but i can't seem to get it to work..My code is below

function getTopic($conn){
    $response = array("error" => 0);
    $qry = "SELECT original_title, content, time FROM topic WHERE vis = 1";
    $result = $conn->prepare($qry);
    $result->execute();
    if($result->rowCount() > 0){
        $output = $result->fetchall();
        $response['text'] = $output['original_title'];
        $response['test'] = $output['content'];
        return json_encode($response);
        //return $output;
    }

Then i try to print the var_dump($response) but i get null values.. Although if i var_dump($output) i get all the rows in an array..accessing the array is the problem here now..i think

NB: i am using PDO

2 Answers 2

1

The problem is the $output is an array that you need to go through. Like:

function getTopic($conn){
    $response = array("error" => 0);
    $qry = "SELECT original_title, content, time FROM topic WHERE vis = 1";
    $result = $conn->prepare($qry);
    $result->execute();
    if($result->rowCount() > 0){
        $output = $result->fetchall();
        foreach ($output as $o){
           $response['text'] = $o['original_title'];
           $response['test'] = $o['content'];
        }
        return json_encode($response);
    }
}

This is for the last response, but if you want all, do:

function getTopic($conn){
    $response = array('error'=>0);
    $qry = "SELECT original_title, content, time FROM topic WHERE vis = 1";
    $result = $conn->prepare($qry);
    $result->execute();
    if($result->rowCount() > 0){
        $output = $result->fetchall();
        foreach ($output as $o){
           $response[] = array('text'=>$o['original_title'],'test'=>$o['content']);
        }
        return json_encode($response);
    }
}

If you are only want one row add a limit to your MySQL statement.

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

4 Comments

The OP does not mention what he uses - mySQLi or PDO.
thanks for responding...but it is still giving the same error..i.e Notice: Undefined index: original_title in C:\wamp\www\api\include\functions.php on line 58 Notice: Undefined index: original_title in C:\wamp\www\api\include\functions.php on line 59 the lines where '$response['text'] = $output['original_title']; $response['test'] = $output['content'];'
The above $response will just contain the last $output record. Might want to use something like $response[] = array('text'=>$output['original_title'],'test'=>$output['content']);
Sorry I made a mistake in the code, try that now. This will only get the last row as the above user mentioned. You can make an array of responses if you want like above.
1

$output is array of results. use a loop or if only one row u need do this:

function getTopic($conn){
$response = array("error" => 0);
$qry = "SELECT original_title, content, time FROM topic WHERE vis = 1";
$result = $conn->prepare($qry);
$result->execute();
if($result->rowCount() > 0){
    $output = $result->fetchall();
    $response['text'] = $output[0]['original_title'];
    $response['test'] = $output[0]['content'];
    return json_encode($response);
    //return $output;
}

2 Comments

This would be if you only want the first row.
true! if there is only one row.

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.