简体   繁体   English

DATE_FORMAT()mysql代码可以在PHPMyAdmin中运行,但不能在PHP中运行

[英]DATE_FORMAT() mysql code will work in PHPMyAdmin but not in PHP

I am writing a reporting method in PHP which is passed 2 parameters $location and $weekYear . 我在PHP中编写一个报告方法,它传递了2个参数$location$weekYear $weekYear is a string that is formatted like 2015-W34 , ie the 34rth week of 2015. the sql code I am trying to run looks like: $weekYear是一个字符串,格式类似于2015-W34 ,即2015-W34的34周。我试图运行的sql代码如下所示:

   $sql = "SELECT 
          CONCAT(tutor.first_name, ' ', tutor.last_name) AS Student,
          CONCAT(tutor.first_name, ' ', tutor.last_name) AS Tutor,
               hours as HoursWorked, 
               DATE_FORMAT(work_date, '%a - %b %d') AS Day,
               DATE_FORMAT(start_time, '%H') AS Hour, 
               DATE_FORMAT(start_time, '%i') AS Minute

          FROM timesheet_entry

          LEFT
          JOIN user
            AS student
            ON student.id = timesheet_entry.student_id

          LEFT
          JOIN user
            AS tutor
            ON tutor.id = timesheet_entry.tutor_id

         WHERE DATE_FORMAT(work_date,'%x-W%v') = '".$weekYear."'
           AND tutoring_location = '".$location."'
           AND work_type = 'instruction'
           AND (instruction_type = '1-on-1' OR instruction_type = 'group')";


        $query = $this->db->query($sql);

When I run the code in php I get no results returned but when I echo out the $sql and then paste that into PhpMyAdmin I get the results I expect. 当我在php中运行代码时,我没有返回任何结果但是当我回显$ sql然后将其粘贴到PhpMyAdmin时,我得到了我期望的结果。

Furthermore if I remove the part 此外,如果我删除该部分

DATE_FORMAT(work_date,'%x-W%v') = '".$weekYear."'

the method runs, and returns more records than I want, but it runs nonetheless. 该方法运行,并返回比我想要的更多的记录,但它仍然运行。

What about my DATE_FORMAT() is causing this query to malfunction? 我的DATE_FORMAT()导致此查询出现故障怎么办?

Any help, advice, and suggestions are greatly appreciated! 非常感谢任何帮助,建议和建议!

我为你的mysql和php使用你的参数$ location和$ weekYear的功能,因为你的参数WEEKYEAR是标准格式和php或mysql不兼容的功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM