Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP PDO Select query returns double values [duplicate]

Tags:

ajax

php

sqlite

pdo

For my inhome temparature sensor i'm using a raspberry pi with php, sqlite, PDO, and HTML.

I've create a table in sqlite

Using

BEGIN;
CREATE TABLE waarden (datum TEXT, tijd TEXT, zone TEXT, lucht REAL, temperatuur REAL);
COMMIT;

My rpi with DHT22 records everything fine so no i've created a webpage accessing the data via AJAX using the following php.file

<?php
function datumConversie($datum){    
    $delen = explode('/',$datum,3);
    $geconverteerd  = $delen[2].$delen[0].$delen[1];
    return $geconverteerd;
}
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

$db = new PDO("sqlite:/home/pi/sensor.db");
$result_array = array();
$date = $_POST["datepicker"];
$waarde = datumConversie($date);
$tijd=""; 
$temperatuur="";
$query = "SELECT datum, tijd, zone,lucht, temperatuur FROM waarden WHERE datum = $waarde";
$result = $db->query($query);

foreach($result as $row)
{
    array_push($result_array, $row);
}
echo json_encode($result_array);
$db = null;
?>

The problem is that when I look in the browser response it seems that values are returned twice. Once with their appropriate field-name and once with their column index. (0 being datum, 1 being tijd etc). See below

{"datum":"20170601","0":"20170601","tijd":"00:01","1":"00:01","zone":"kelder","2":"kelder","lucht":"53.0","3":"53.0","temperatuur":"24.3","4":"24.3"}, {"datum":"20170601","0":"20170601","tijd":"00:06","1":"00:06","zone":"kelder","2":"kelder","lucht":"53.1","3":"53.1","temperatuur":"24.3","4":"24.3"}, {"datum":"20170601","0":"20170601","tijd":"00:11","1":"00:11","zone":"kelder","2":"kelder","lucht":"53.1","3":"53.1","temperatuur":"24.2","4":"24.2"},

How can I avoid this. I've tried several conversions which can work in the end but that is just patching some flaw without addressing the cause?

Any suggestions

like image 492
MaPo Avatar asked Mar 18 '26 20:03

MaPo


2 Answers

Set the default fetch mode like so

$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); 

Or

$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); 

before running the fetch

$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
foreach($result as $row) {
    array_push($result_array, $row);
}

Or replace with a one liners using fetchAll() and use prepared and parameterized statements as well to mitigate against SQL Injection Attack

$query = "SELECT datum, tijd, zone,lucht, temperatuur 
            FROM waarden 
            WHERE datum = :datum";
$result->prepare($query);

$result->execute([':datum'=>$waarde]);

$result_array = $result->fetchAll(PDO::FETCH_ASSOC);

The parameter to fetchAll(PDO::FETCH_ASSOC) controls how results will be returned.

like image 52
RiggsFolly Avatar answered Mar 21 '26 09:03

RiggsFolly


Try this sample code...

$sth = $db->prepare("SELECT datum, tijd, zone,lucht, temperatuur FROM waarden WHERE datum = :datum");
$sth->execute(array('datum' => $waarde));
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

echo '<pre>';
print_r($result);
echo '</pre>';
like image 39
Suresh Avatar answered Mar 21 '26 11:03

Suresh