2011-01-19 80 views
1

我有一個PostgreSQL 8.4.6表,其中玩家手中多達32個卡由32位表示:PHP:鑄造長期或無符號整數與32位

# \d pref_hand 
       Table "public.pref_hand" 
Column |   Type    | Modifiers 
--------+-----------------------------+--------------- 
id  | character varying(32)  | 
hand | bigint      | not null 
money | integer      | not null 
stamp | timestamp without time zone | default now() 
Check constraints: 
    "pref_hand_hand_check" CHECK (hand > 0) 

(你可以看到,我已經通過使用bigint已經被騙了)。

My script(請滾動到底部查看呈現的卡片)在64位CentOS 5.5 Linux上正常工作。但是,當我將它複製到我的32位開發虛擬機時,它會失敗。

下面是代碼摘錄:

$sth = $db->prepare('select hand, money 
        from pref_hand where id=? 
        order by stamp desc'); 
$sth->execute(array($id)); 
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) { 
     print ('<div>'); 
     for ($i = count($CARDS) - 1; $i >= 0; $i--) { 
       $card = $CARDS[$i]; 
       $color = (strpos($card, '♥') > 0 || 
          strpos($card, '♦') > 0 ? 'red' : 'black'); 

       if (23 == $i || 15 == $i || 7 == $i) 
         print('&nbsp;'); 

       if ((1 << $i) & $row['hand']) 
         printf('<span class="%s">%s</span>', $color, $CARDS[$i]); 
     } 
     print ('</div>'); 
    } 

,我認爲如果((1 < < $ I)& $行[ '手'])未能有(對不起,我會如果我沒有收到答案,以後再準備一個縮減的測試用例...)

我的問題是:如何處理最好的PHP情況?我需要鑄造$ row ['hand']unsigned intlong以某種方式 - 使AND位操作符再次工作,但我無法在PHP文檔中找到這些數據類型。

謝謝! Alex

回答

0

您可以使用GMP來存儲該號碼,然後通過gmp_testbit測試該卡是否可用。雖然這顯然需要安裝GMP(並且它通常不在共享主機上)。

+0

請問您真的選擇了什麼?你使用過GMP還是切換到另一種數據結構?或者其他解決方案? – NikiC 2011-01-23 13:32:41