views:

39

answers:

1

I keep getting an "Notice: Undefined index: did" error with this query, and I'm not understanding why... I'm much more used to mysql, so, maybe the syntax is wrong?

This is the php query code:

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;
    }

When I run that query at the pg prompt, I get results:

"(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")"
A: 

You must use pg_fetch_assoc() function if you want to get associative array in $r.

Mihail Dimitrov
pg\_fetch\_array() by default returns the numeric _and_ the associative indices, see http://docs.php.net/pg_fetch_array
VolkerK