php
  • mysql
  • sql
  • 2011-04-02 54 views 1 likes 
    1

    我試圖選擇滿足以下一系列條件的數據。在MySQL中使用set time_zone的問題

    問題是,我位於新西蘭(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..

    回答

    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; 
    

    希望有幫助嗎?

    +0

    謝謝@jay,據我所知,設置time_zone必須是同一查詢的一部分 – Jamie 2011-04-02 22:54:42

    +0

    看看[http://dev.mysql.com/doc/refman/5.5/en/time-zone- support.html](http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html) 你會看到你可以改變每個連接的時區:) – Jay 2011-04-04 16:37:39

    相關問題