Reputation: 2749
New to php, trying to learn about arrays.
What is wrong with the code below? I need to echo out the results in order to pass them to another page.
<?php
$stmt = $conn -> prepare("
SELECT
MONTHNAME(TimeStamp), COUNT(*)
FROM
transactions
WHERE
TimeStamp BETWEEN '2014-01-01' AND '2014-12-31'
GROUP BY EXTRACT(MONTH FROM TIMESTAMP);");
$stmt -> execute();
$stmt -> bind_result($month, $days);
while ($stmt -> fetch()) {
$data[] = array(
'month' => $month,
'day' => $day);
}
print_r($data);
$data = array();
foreach($data as $row) {
echo $row['month'];
echo $row['day'];
}
$stmt->close();
?>
When I print_r($data)
I can see the array fine in the following format.
Array ( [0] => Array ( [month] => January [day] => 323537 ) [1] => Array ( [month] => February [day] => 217304 ) [2] => Array (...
Upvotes: 0
Views: 44
Reputation: 325
print_r($data);
**$data = array();**
foreach($data as $row) {
echo $row['month'];
echo $row['day'];
}
You print_r your $data, then you set it to an empty array with $data = array(), and in the foreach you are trying to go through an empty array.
Solution:
print_r($data);
foreach($data as $row) {
echo $row['month'];
echo $row['day'];
}
$data = array();
Upvotes: 1