0

I have the following code:

$query6 = "SELECT TIMESTAMP As sDate, COUNT( TIMESTAMP ) AS Total 
           FROM tresults GROUP BY TO_DAYS( timestamp ) ";

$result6 = $mysqli->query($query6);

$rows = array();

while($row6 = $result6->fetch_array())
    {
        $rows[]=$row6;
    }

Whilst this works, the output is as follows but isn't what I expected:

Array ( [0] => Array ( [0] => 2014-05-14 02:11:16 [1] => 1 ) 
[1] => Array ( [0] => 2014-05-19 05:05:17 [1] => 76 ) 
[2] => Array ( [0] => 2014-05-20 00:28:41 [1] => 35 ) 
[3] => Array ( [0] => 2014-05-21 01:24:01 [1] => 25 ) )

I had expected the result to be in a single array not multi-arrays.

Clearly the output is correct based on my code but what I am struggling with is then (within PHP) looping through the above array and echo'ing the output.

If anyone can advise on the code need to either loop through the above correctly or advise on changing the above so the output is within one array (which I know how to loop through) - that would be appreciated.

2
  • Which row do you need? If you need to print details of every row then don't create a new array, just use echo on the array that you are looping over Commented Jun 5, 2014 at 16:06
  • Where did I ask for a single value to be stored? Why the downvote? Commented Jun 5, 2014 at 16:07

1 Answer 1

1

You can echo out the content in the while loop instead of inserting each row into a giant array.

while($row = $result6->fetch_array())
{
    echo $row[0] . ' ' . $row[1];
}

If you want to deal with the giant array after the while loop, you can use a foreach loop:

foreach ($rows as $row => $record)
{
    echo $record[0] . ' ' . $record[1];
}

The foreach loops through each array and returns the value as $record (in this case it is the inner array).

Sign up to request clarification or add additional context in comments.

1 Comment

appreciate you taking the time to respond. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.