Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 identical elements returned in array, after mysql query, why?

Ive made a mysql query using PDO like this

$timestamp_query = "SELECT timestamp FROM change_me;";

foreach ($conn->query($timestamp_query) as $times ) {
    print_r($times); 
    echo "<br>";
 }

But this is giving me back a set of arrays like this

Array ( [timestamp] => 2013-05-24 17:55:04 [0] => 2013-05-24 17:55:04 )
Array ( [timestamp] => 2013-05-24 18:00:05 [0] => 2013-05-24 18:00:05 )
Array ( [timestamp] => 2013-05-24 18:05:04 [0] => 2013-05-24 18:05:04 ) 

Why is this giving me 2 identical elements in each array, as opposed to just one ?

like image 619
sam Avatar asked Jul 04 '26 23:07

sam


1 Answers

It returns an associative array to you so you can reference the results by either field name or index.

E.g. $times['timestamp'] or $times[0]

like image 57
j08691 Avatar answered Jul 07 '26 13:07

j08691



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!