0

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 (... 
2
  • 5
    You realize that you overwrite your array with an empty array: $data = array(); ?! Commented Aug 21, 2015 at 16:03
  • In order to pass it in another page you must use header("Location:your_path_to_the_second_page/.$your_variables"); Commented Aug 21, 2015 at 16:06

1 Answer 1

1
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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.