1
我正在研究一個腳本,該腳本將使用PHP和MySQL顯示員工時間表。 我使用PHP 5.3.13和MySQL 5.5.24使用PHP和MySQL中的日期查詢
我想要做的是填充一個表,將有日期和工作人員的開始時間,如果狀態是ACTV,或狀態如果工作人員要麼休假要麼休假。
的代碼如下:
<body>
<center>
<?php
include "../../scripts/php/dbcon/dbcon.php";
$schedule_table = "cc_schedule";
$date_query="SELECT DISTINCT DATE(schedule_date) FROM `$schedule_table`";
$date_result= mysql_query($date_query);
$datenum = mysql_numrows($date_result);
?>
<!-- Table Head -->
<center>
<table cellpadding="0" cellspacing="0">
<tr>
<th><center>No.</center></th>
<th><center>Name</center></th>
<th><center>Employee ID</center></th>
<?php
$d =0;
while ($d < $datenum){
$dt = mysql_result($date_result,$d,"DATE(schedule_date)");
$date = date_create($dt);
$dd = $date;
echo "<th><center>". date_format($dd, 'd-M-Y')."</th><center>";
$d++;
}
?>
</tr>
<!-- Table Body -->
<?php
$names_query="SELECT StaffName, ID
from users
WHERE groupname = 'call center'";
$name_result = mysql_query($names_query);
$namenum = mysql_numrows($name_result);
$schedule_query="SELECT *
FROM `$schedule_table`
ORDER BY `schedule_date` ASC,`sstatus` ASC,`start_time` ASC,`employee_id` ASC";
$schedule_result=mysql_query($schedule_query);
$schedule_num = mysql_numrows($schedule_result);
$n = 1;
while ($n < $namenum){
?>
<tr>
<?php
echo"<td><center>".($n)."</td></center>";
echo"<td><center>".mysql_result($name_result,$n,"StaffName")."</td></center>";
echo"<td><center>".mysql_result($name_result,$n,"ID")."</td></center>";
$d=1;
{
$emp_id = mysql_result($name_result,$n,"ID");
$dt = mysql_result($date_result,$d,"DATE(schedule_date)");
$date = date_create($dt);
$dd = date_format($date, 'Y-m-d');
echo $emp_id;
echo DATE($dd);
while ($d < $datenum) {
$schedule_query ="SELECT *
from $schedule_table
WHERE `employee_id` = $emp_id AND `schedule_date`='$dd'";
$schedule_result = mysql_query($schedule_query);
$schedule = mysql_result($schedule_result,0,"start_time");
$status = mysql_result($schedule_result,0,"sstatus");
if ($status =="ACTV"){
'<td class="'.$status.'"><center>'.$schedule.'</td></center>';
}
else {
echo '<td class="'.$status.'"><center>'.$status."</td></center>";
}
}
$d++;
}
?>
</tr>
<?php
$n++;
}
?>
</table>
</center>
</body>
代碼不返回任何東西。它每次運行時都會返回不同的錯誤。我懷疑查詢有問題。我只是不知道它..
任何幫助表示讚賞。
謝謝。
穆罕默德。
你能提供一些你得到的錯誤嗎? –
[請不要在新代碼中使用'mysql_ *'函數](http://stackoverflow.com/q/12859942)。它們不再被維護,並且已經開始棄用過程,請參閱[紅盒子](http://php.net/mysql-connect)。請改爲了解[準備好的語句](http://en.wikipedia.org/wiki/Prepared_statement),並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php。淨/ mysqli的); [這篇文章](http://php.net/mysqlinfo.api.choosing)將幫助你決定。如果你選擇PDO,[這裏是一個很好的教程](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers)。 –
您是否已經調試了$ date_result的輸出和其他語句(例如var_dump)以查看您從數據庫中獲得的內容?如果沒有,請嘗試修復SQL語句。用普通的SQL編寫在phpmyadmin或在mysql控制檯上... 我強烈建議您分開您的業務和查看代碼... – awenkhh