[英]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. 显然,根据我的代码,输出是正确的,但是我正在努力的是(在PHP中)循环遍历上述数组并
echo'ing
显输出。
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循环中回显内容,而不是将每一行插入一个巨大的数组中。
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: 如果要在while循环之后处理巨型数组,可以使用foreach循环:
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). foreach循环遍历每个数组,并将值返回为$ record(在这种情况下,它是内部数组)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.