Can someone please help me how to get my MySQL column names out in my while loop? My code goes like this:
if (mysqli_connect_errno()) {
echo "MySQL failed: " . mysqli_connect_errno();
}
if (isset($_GET['date'])) {
$get_date = $_GET['date'];
$result = mysqli_query($conn, "SELECT * FROM stats WHERE form_date = '$get_date'");
$col = array();
while ($row = mysqli_fetch_array($result)) {
$col[] = $row['year'];
$col[] = $row['month'];
$col[] = $row['day'];
$col[] = $row['weekday'];
$col[] = $row['form_date'];
$col[] = $row['name'];
$col[] = $row['count'];
$col[] = $row['lang'];
}
echo "<pre>";
print_r($col);
echo "</pre>";
mysqli_close($conn);
} else {
echo "Please set a date value in URL parameter ?date=[YYYY-MM-DD]";
}
My returned array then looks like this:
Array
(
[0] => 2014
[1] => 01
[2] => 01
[3] => Wednesday
[4] => 2014-01-01
[5] => Michael K
[6] => 41
[7] => SE
[8] => 2014
[9] => 01
[10] => 01
[11] => Wednesday
[12] => 2014-01-01
[13] => Nicklas S
[14] => 40
[15] => DK
[16] => 2014
[17] => 01
[18] => 01
[19] => Wednesday
[20] => 2014-01-01
[21] => Jesper S
[22] => 5
[23] => SE
)
However I would like my array to get returned like this:
Array
(
[year] => 2014
[day] => 01
[month] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Michael K
[count] => 41
[lang] => SE
[year] => 2014
[month] => 01
[day] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Nicklas S
[count] => 40
[lang] => DK
[year] => 2014
[month] => 01
[day] => 01
[weekday] => Wednesday
[form_date] => 2014-01-01
[name] => Jesper S
[count] => 5
[lang] => SE
)
So the keys in my array matches the column names I have in my database. I just can't seem to wrap my head around how I do this. Can someone help? :-)