-1

I wanted to add all arrays from my database in one json encode and decode it later on.. but I ran into a thing which I don't know how to fix.

I'm trying to add all arrays from the sql query in one array and then encode that one array with all the arrays inside it.

My current code:

        $dbh = new PDO('mysql:host='.$this->server.';dbname='.$this->database.'', $this->user, $this->pass);
        $result = array();
        foreach($dbh->query('SELECT * from bier') as $row) {
           $result[] += $row;
        }
        print json_encode($result);

Updated code:

function getData()
    {
    try {

        $dbh = new PDO('mysql:host='.$this->server.';dbname='.$this->database.'', $this->user, $this->pass);
        $result = array();
        foreach($dbh->query('SELECT * from bier') as $row) {
            $result[] = $row;
        }
        $json = json_encode($result, JSON_PRETTY_PRINT);
        var_dump($json);

    } catch (PDOException $e) {
        print "Error!: " . $e->getMessage() . "<br/>";
        die();
    }
}

Currently returns: Boolean False

1 Answers1

0

In PHP, the += compound operator is used to "add & assign"; however, this does not work as you'd expect for inserting items into an array.

Your code should work if you remove the + from that line as @Rajdeep pointed out.

Here is your code, updated with some changes for clarity:

try
{
    $data = new PDO('mysql:host='.$this->server.';dbname='.$this->database.'', $this->user, $this->pass);
    $data->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $list = [];

    foreach($data->query('SELECT * from bier') as $item)
    { $list[] = $item; }

    $json = json_encode($list, JSON_PRETTY_PRINT);

    echo $json;
}
catch (PDOException $fail)
{
    echo $fail->getMessage();
    exit;
}

As you probably notice; the [] = character sequence after a variable name (that is an array) inserts the new item in the array being assigned.

The try --> catch block should indicate if there was a problem.

Here's more info on how to check for errors: http://php.net/manual/en/pdo.connections.php

  • Thanks alot! but it still seems that if i try to echo or print it nothing will be shown :/ – BananaCoder May 10 '16 at 13:32
  • Make sure that the data-base connection settings are correct, and make sure there is data in the data-base table `bier` as suggested by your `select` query. –  May 10 '16 at 13:35
  • That's all fine i've testted it – BananaCoder May 10 '16 at 13:37
  • @BananaCoder :: I've updated the answer and added a link that explains PDO connections, and also shows how to detect errors, as this may be what's causing your results to be empty. –  May 10 '16 at 13:41
  • @BananaCoder :: I've added the code to force throwing errors if any, so, the try / catch should handle that if errors are thrown, and display what went wrong. Don't forget to test the code, this latest code update here will most likely work - or at least show you what went wrong. –  May 10 '16 at 13:57
  • I've updated the topic so you can see my current code, Now if i try to echo it or var dump it it will return Boolean false I've no clue why it's saying that :/ – BananaCoder May 10 '16 at 14:01
  • @BananaCoder :: there must be something obvious you're missing; look through your code; have a look here on how exactly it works: http://php.net/manual/en/pdo.error-handling.php .. if you're still having issues, then, try using **mysqli** instead of _PDO_ –  May 10 '16 at 14:06
  • @BananaCoder :: In your updated code, I see you did not add the `$data->setAttribute(...)` code; so, it will not show any errors, that's why you're getting `false`.. Have a look at my updated answer, copy+paste & test, it should work. –  May 10 '16 at 14:10