2010-03-14 79 views
2

我不斷收到一個「通知:未定義的索引:做」這個查詢錯誤,我不明白爲什麼...我更習慣於mysql,所以,也許語法是錯誤?postgresql的幫助與PHP循環

這是PHP的查詢代碼:

function get_demos() { 
     global $session; 
     $demo = array(); 
     $result = pg_query("SELECT DISTINCT(did,vid,iid,value) FROM dv"); 
     if(pg_num_rows($result) > 0) { 
      while($r = pg_fetch_array($result)) { 
       switch($r['did']) { 
        case 1: 
         $demo['a'][$r['vid']] = $r['value']; 
         break; 
        case 2: 
         $demo['b'][$r['vid']] = $r['value']; 
         break; 
        case 3: 
         $demo['c'][$r['vid']] = $r['value']; 
         break; 
       } 
      } 
     } else { 
      $session->session_setMessage(2); 
     } 

     return $demo; 
    } 

當我運行該查詢在PG提示,我得到的結果:

"(1,1,1,"A")" 
"(1,2,2,"B")" 
"(1,3,3,"C")" 
"(1,4,4,"D")" 
"(1,5,5,"E")" 
"(1,6,6,"F")" 
"(1,7,7,"G")" 
"(1,8,8,"H")" 
"(1,9,9,"I")" 
"(1,10,A,"J")" 
"(1,11,B,"K")" 
"(1,12,C,"L")" 
"(1,13,D,"M")" 
"(2,14,1,"A")" 
"(2,15,2,"B")" 
"(2,16,0,"C")" 
"(3,17,1,"A")" 
"(3,18,2,"B")" 
"(3,19,3,"C")" 
"(3,20,4,"D")" 
"(3,21,5,"E")" 
"(3,22,6,"F")" 
"(3,23,7,"G")" 
+0

在你的while循環中嘗試'var_dump($ r)' – 2010-03-14 16:12:46

+0

實際上,我們發現DISTINCT是它不喜歡的東西...... – TwixxyKit 2010-03-14 16:26:55

+0

然後請給你自己的問題寫一個答案並接受它(在48 ?hours) – VolkerK 2010-03-14 17:59:11

回答

0

,如果你想獲得聯想必須使用pg_fetch_assoc()功能數組$ r。

+1

pg \ _fetch \ _array()默認返回數字_and_關聯索引,請參閱http://docs.php.net/pg_fetch_array – VolkerK 2010-03-14 17:56:59