我有兩個表:子查詢在PHP中的數據提取到Excel
biometrics - records in and out of employees
emp - employee records
在我的生物識別表中,有一個狀態。
0 - if the employee went out
1 - if the employee went in
員工可以在一天中擁有多個0和1的狀態,因爲您在休息時也計時並超時。所以狀態可以是這樣的:
time_created status
9:00 1 --- time in
12:00 0 --- time out for break
13:00 1 --- time in again after break
18:00 0 --- time out
所以員工可以有多個休息時間。
我已經有這個正確的查詢,但這裏是問題。我正在製作一個記錄所有休息的程序。當我將它解壓縮到Excel時,「超時」列和「timediff」(顯示員工在他/她的休息時間中花費的總小時數和分鐘數的列)沒有顯示。這兩列是子查詢的產品。
這裏是我的代碼:
if(isset($_POST['timediff'])){
header("Content-type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename=Export_Date.csv');
$con = mysql_connect("localhost", "root");
if(!$con){
echo "Error connection";
}
$select_db = mysql_select_db('sample', $con);
if(!$select_db){
echo "Error to select database";
}
mysql_set_charset("utf8", $con);
$myquery2 = mysql_query("select g.empno, emp.emp_fname, emp.emp_fname, emp.position, emp.office , g.date_created, min(g.time_created), g.timeout, timediff(g.timeout, min(g.time_created))
FROM
(
select id_biometrics, empno,date_created, time_created, (
SELECT min(s.time_created) FROM biometrics s
WHERE s.status like '0' AND s.time_created > base.time_created
AND s.empno= base.empno AND s.date_created = base.date_created
ORDER BY s.time_created ASC) as timeout
from biometrics base
where base.status like '1'
) g, emp
WHERE emp.empno = g.empno AND NOT (g.timeout = (SELECT min(time_created) FROM biometrics where status like '1'))
GROUP BY g.empno, g.timeout
ORDER BY g.date_created;");
//While loop to fetch the records
$contents = "empno,emp_fname,emp_lname,position,office,date_created,timeout,timein,timediff\n";
while($row = mysql_fetch_array($myquery2))
{
$contents.=$row['empno'].",";
$contents.=$row['emp_fname'].",";
$contents.=$row['emp_lname'].",";
$contents.=$row['position'].",";
$contents.=$row['office'].",";
$contents.=$row['date_created'].",";
$contents.=$row['min(g.time_created)'].",";
$contents.=$row['g.timeout'].",";
$contents.=$row['timediff(g.timeout, min(g.time_created)']."\n";
}
$contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
print $contents_final;
}
?>
的 「timein」 的顯示。只有最後兩列:「超時」(g.timeout)和「timediff」沒有顯示。我不知道什麼是錯的,因爲在MySQL Workbench中,這個查詢是完美的。
右鍵我想如果你將'PDO :: FETCH_ASSOC'傳遞給fetch方法(請參閱文檔以仔細檢查名稱),你可以通過名稱而不是數字索引來訪問列,至少現在是工作!不要忘記自己接受你自己的答案。 – halfer
好吧,我現在就開始學習PDO和mysqli。hahahaha。再次感謝。:D – nicole101