在MySQL中使用set time_zone的问题

时间:2011-04-02 22:43:50

标签: php mysql sql

我正在尝试选择满足以下条件的数据。

问题是,我位于新西兰(UTC + 12)并且服务器是共享的 - 即时区不可更新(并且在UTC-8)。我正在尝试在查询开始时使用SET time_zone但它似乎不起作用。

非常感谢任何想法。

function getIndivDeal($id) {
     $result = mysql_query("
       SET time_zone = '+19:00'
       SELECT i, company, deal_type, deal_title, deal_message, valid_time_start, 
         valid_time_end, DATE_FORMAT(valid_expiry, '%D %b %y'), valid_mon, 
         valid_tue, valid_wed, valid_thu, valid_fri, valid_sat, valid_sun
       WHERE i = '" .mysql_real_escape_string($id). "'
       AND valid_expiry > CURDATE()
       AND valid_time_start >= CURTIME()
       AND valid_time_end < CURTIME()
       AND CASE DAYOFWEEK(CURDATE())
         WHEN 1 THEN valid_mon
         WHEN 2 THEN valid_mon
         WHEN 3 THEN valid_tue
         WHEN 4 THEN valid_wed
         WHEN 5 THEN valid_thu
         WHEN 6 THEN valid_fri
         WHEN 7 THEN valid_sat
       END = 1
    ");
    return $result;
}

// $result is used in another file like so:
$id = 2501;
getIndivDeal($id)
$result = getIndivDeal($id);
if (mysql_num_rows($result)) {
    while($row = mysql_fetch_array($result)) {
        echo $row["TIME_FORMAT(valid_time_start, '%l:%i %p')"];
    }
}

在我添加SET time_zone之前它正常工作,然后它会发出以下错误:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in..

1 个答案:

答案 0 :(得分:2)

也许它会产生一个无效的mysql查询!你试过吗

mysql_query("SET time_zone = '+19:00'");
$result = mysql_query("
   SELECT i, company, deal_type, deal_title, deal_message, valid_time_start, 
     valid_time_end, DATE_FORMAT(valid_expiry, '%D %b %y'), valid_mon, 
     valid_tue, valid_wed, valid_thu, valid_fri, valid_sat, valid_sun
   WHERE i = '" .mysql_real_escape_string($id). "'
   AND valid_expiry > CURDATE()
   AND valid_time_start >= CURTIME()
   AND valid_time_end < CURTIME()
   AND CASE DAYOFWEEK(CURDATE())
     WHEN 1 THEN valid_mon
     WHEN 2 THEN valid_mon
     WHEN 3 THEN valid_tue
     WHEN 4 THEN valid_wed
     WHEN 5 THEN valid_thu
     WHEN 6 THEN valid_fri
     WHEN 7 THEN valid_sat
   END = 1
");
return $result;

希望有帮助吗?