我正在尝试像这样执行查询
$conn = Doctrine_Manager::connection()->getDbh();
$query = "SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'GROUP_CONCAT(IF(DATE(punch_in_user_time) = ', QUOTE(DATE(punch_in_user_time)), ', TIME(punch_in_user_time), NULL)) AS ', QUOTE(DATE(punch_in_user_time)),
',GROUP_CONCAT(IF(DATE(punch_out_user_time) = ', QUOTE(DATE(punch_in_user_time)), ', TIME(punch_out_user_time), NULL)) AS ', QUOTE(DATE(punch_in_user_time))
) ORDER BY punch_in_user_time
) INTO @sql
FROM ohrm_attendance_record
where DATE(punch_in_user_time) between QUOTE(" . $fromDate .") and QUOTE(" . $toDate . ")
ORDER BY punch_in_user_time ;
SET @@group_concat_max_len = 10000;
SET @sql = CONCAT('select emp_number, ', @sql,
' from hs_hr_employee e LEFT JOIN ohrm_attendance_record a ON a.employee_id = e.emp_number group by emp_number
where true ');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
$statement = $conn->query($query);
return $statement->fetchAll(PDO::FETCH_ASSOC);
但是会收到这样的错误
01/28/15 18:08:41,494 [3268] ERROR filter.ExceptionCatcherFilter - Uncaught Exception: exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in C:\xampp\htdocs\orangehrm\symfony\plugins\orangehrmAttendancePlugin\lib\dao\AttendanceDao.php:553
查询在MySQL上完美运行,我的代码出了什么问题?
请帮忙!
答案 0 :(得分:0)
尝试将每个单独的陈述单独发送给学说 对于不返回结果的语句(如SET)也使用exec()。
尝试这样的事情:
$conn = Doctrine_Manager::connection()->getDbh();
$query = "SET @sql = NULL;";
$conn->exec($query);
$query = "SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'GROUP_CONCAT(IF(DATE(punch_in_user_time) = ', QUOTE(DATE(punch_in_user_time)), ', TIME(punch_in_user_time), NULL)) AS ', QUOTE(DATE(punch_in_user_time)),
',GROUP_CONCAT(IF(DATE(punch_out_user_time) = ', QUOTE(DATE(punch_in_user_time)), ', TIME(punch_out_user_time), NULL)) AS ', QUOTE(DATE(punch_in_user_time))
) ORDER BY punch_in_user_time
) INTO @sql
FROM ohrm_attendance_record
WHERE DATE(punch_in_user_time) between QUOTE(" . $fromDate .") and QUOTE(" . $toDate . ")
ORDER BY punch_in_user_time ;";
$conn->exec($query);
$query = "SET @@group_concat_max_len = 10000;";
$conn->exec($query);
$query = "SET @sql = CONCAT('select emp_number, ', @sql,
' from hs_hr_employee e LEFT JOIN ohrm_attendance_record a ON a.employee_id = e.emp_number group by emp_number
where true ');";
$conn->exec($query);
$query = "PREPARE stmt FROM @sql;";
$conn->exec($query);
$query = "EXECUTE stmt;";
$statement = $conn->query($query);
$query = "DEALLOCATE PREPARE stmt;";
$conn->exec($query);
return $statement->fetchAll(PDO::FETCH_ASSOC);
我认为它应该按原样运作。