PHP MySQL select 问题
PHP MySQL select issue
我有一个名为 emplyees 的数据库,结构如下:
id name entrydate totalhours
1 test 11/11/2015 8
2 test2 11/11/2015 7
3 test 11/12/2015 8
4 test2 11/12/2015 9
我想把它变成这样
id name 11/11/2015 11/12/2015
1 test 8 8
2 test2 7 9
我试了好几种方法都没有结果我该怎么办?
提前致谢!
一旦你确定了你的日期,这样的事情应该在 PHP...
<?php
require('path/to/connection/stateme.nts');
$query = "
SELECT name
, entrydate
, totalhours
FROM my_table
ORDER
BY name;
";
$result = mysqli_query($db,$query);
$array = array();
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
}
foreach ($array as $element)
$newArray[$element["name"]][] = array($element["entrydate"]=>$element["totalhours"]);
print_r($newArray);
?>
输出:
Array
(
[test] => Array
(
[0] => Array
(
[2015-11-11] => 8
)
[1] => Array
(
[2015-11-12] => 8
)
)
[test2] => Array
(
[0] => Array
(
[2015-11-11] => 7
)
[1] => Array
(
[2015-11-12] => 9
)
)
)
...可以轻松吐出到 table 或 json
我有一个名为 emplyees 的数据库,结构如下:
id name entrydate totalhours
1 test 11/11/2015 8
2 test2 11/11/2015 7
3 test 11/12/2015 8
4 test2 11/12/2015 9
我想把它变成这样
id name 11/11/2015 11/12/2015
1 test 8 8
2 test2 7 9
我试了好几种方法都没有结果我该怎么办? 提前致谢!
一旦你确定了你的日期,这样的事情应该在 PHP...
<?php
require('path/to/connection/stateme.nts');
$query = "
SELECT name
, entrydate
, totalhours
FROM my_table
ORDER
BY name;
";
$result = mysqli_query($db,$query);
$array = array();
while($row = mysqli_fetch_assoc($result)){
$array[] = $row;
}
foreach ($array as $element)
$newArray[$element["name"]][] = array($element["entrydate"]=>$element["totalhours"]);
print_r($newArray);
?>
输出:
Array
(
[test] => Array
(
[0] => Array
(
[2015-11-11] => 8
)
[1] => Array
(
[2015-11-12] => 8
)
)
[test2] => Array
(
[0] => Array
(
[2015-11-11] => 7
)
[1] => Array
(
[2015-11-12] => 9
)
)
)
...可以轻松吐出到 table 或 json