0

I have the following data in my database:

data

I want to have the date in the JSON format below:

{
    "labels": ["12.11.2016", "13.11.2016", "14.11.2016", ...],
    "temperature": ["12", "35", "27", ...],
    "humidity": ["56", "70", "87", ...]
}

My current code is:

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        // set the PDO error mode to exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);

        //Select the requested data
        $sql = "SELECT date, temperature, humidity FROM `TestData` ORDER BY date ASC ";

        $statement = $conn->prepare($sql);
        $statement->execute();
        $result = $statement->fetchAll(PDO::FETCH_ASSOC);

        //Values returned for JavaScript
        $labels = array();
        $temperature = array();
        $humidity = array();
        $data = array();

        foreach ($result as $row)
        {
            array_push($labels, $row['date'] );
            array_push($temperature, $row['temperature']);
            array_push($humidity, $row['humidity']);
        }
        $result = null;

        //Load data in one single array
        $data['labels'] = $labels;
        $data['temperature'] = $temperature;
        $data['humidity'] = $humidity;
        //echo $data;
        //echo json_encode($labels , JSON_FORCE_OBJECT);
        echo json_encode($data);

but this gives me only the following sh*t: shit

Does anyone have an idea here? I just can't get it working even if I already searched the internet for like 5 hours :/

2
  • 1
    What's the output of $result ? var_dump($result) Commented Oct 2, 2016 at 20:07
  • Output is here Commented Oct 2, 2016 at 20:12

2 Answers 2

2

Try:

array_push($labels, $row['DATE'] );
array_push($temperature, $row['TEMPERATURE']);
array_push($humidity, $row['HUMIDITY']);

Looks like all you column names are upper case because of $conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);.

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

4 Comments

Haha, WTF... This worked for me :D Ok, thank you @Shawn. Result is now in the right format :)
What exactly is the issue behind this? Can you explain this to me. I don't think, I used upper case descriptions for my columns... :/
Actually it's this line: $conn->setAttribute(PDO::ATTR_CASE, PDO::CASE_UPPER);
Ah, f*ck, yes... Didn't think about this anymore :/
1

The output of the fetch is a key to value pair. The key being the row number.

Also, your column names seem to be uppercase from the var dump that you posted.

Try this to fetch the values correctly:

foreach ($result as $key => $row)
{
    array_push($labels, $row['DATE'] );
    array_push($temperature, $row['TEMPERATURE']);
    array_push($humidity, $row['HUMIDITY']);
}

5 Comments

Nope, still gives me those null values... link
foreach ($result as $key => $row) or foreach($result as $key) doesn't matter in this case.
Yeah, that's what i realized as well, it really was the issue with the upper case letters
Is now also the right answer... I cannot check 2 of them :(
@FranzHuber23 Shawn was first :)

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.