ps34
ps34

Reputation: 183

MySQL to JSON Encode Error / No Results

I know this type of question has been asked a couple of times but my problem is a little bit different and I can't figure out of to fix this. I've checked other similar questions but couldn't find a solution since everything seems correct.

I have a database table named KalkanliMekanlar and it has 3 tables inside it. Right now, I want to encode the contents of Mekanlar table. It has 5 columns and 22 rows inside it.

When I run the following PHP code, I see no result. I'd be grateful if you can help me with this.

I don't know if it helps but my server is on digitalocean.

Thank you very much for your help!

PHP Code:

$sql = "SELECT * FROM Mekanlar";

$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$emparray[] = array();

while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}

echo json_encode($emparray);

mysqli_close($connection);

Thank you very much for your help!

Upvotes: 1

Views: 563

Answers (2)

YvesLeBorg
YvesLeBorg

Reputation: 9079

after doing @RhinoDevel's suggestion, also do this

while($row =mysqli_fetch_assoc($result))
{
     array_push($emparray,$row);
}

EDIT : following comments from Rhino,

    $array[] = array ();
    for ($i = 0 ; $i < 5 ; $i++) {
        $array[] = $i;
    }
    $logger->info("OP : " . json_encode($array));
    $array = array ();
    for ($i = 0 ; $i < 5 ; $i++) {
        $array[] = $i;
    }
    $logger->info("RD : " . json_encode($array));

    $array = array ();
    for ($i = 0 ; $i < 5 ; $i++) {
        array_push($array , $i);
    }
    $logger->info("YL : " . json_encode($array));

yields :

2015-07-17T06:59:42-04:00 TestPatient.api           INFO  OP : [[],0,1,2,3,4]
2015-07-17T06:59:42-04:00 TestPatient.api           INFO  RD : [0,1,2,3,4]
2015-07-17T06:59:42-04:00 TestPatient.api           INFO  YL : [0,1,2,3,4]

Upvotes: 1

RhinoDevel
RhinoDevel

Reputation: 712

Replace this:

$emparray[] = array();

with that:

$emparray = array();

Upvotes: 2

Related Questions