简体   繁体   中英

MySQL mysqli - returning rows in an array

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.

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM