Re: make keys of a associative array DB field names?

From: Date: Fri, 14 Apr 2006 20:09:33 +0000
Subject: Re: make keys of a associative array DB field names?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-234019@lists.php.net to get a copy of this message
At 12:54 PM 4/14/2006, Bing Du wrote:
Would anybody remind me how this should be achieved? This returns all the names of the fields. while( ($f = odbtp_fetch_field( $qry )) ) {
        echo $f->name . "\n";
    }
This returns the query results: while( ($rec = odbtp_fetch_array($qry)) ) { foreach ($rec as $var) {
        echo "$var\n";
} } How should I connect them together? Say, the fields are 'name', 'age', 'score'. I want to print out $rec['name'], $rec['age'] and $rec['score'] rather than using $rec[0], $rec[1] and $rec[2].
Here's one way:
        $oHandle = mysql_query($sSQL);
                if (!$oHandle)
                {
                        // error trapping goes here
                }
        // cycle through the records:
        while ($aDataRow = mysql_fetch_array($oHandle, MYSQL_ASSOC))
        {
                // cycle through the fields in each record:
                foreach ($aDataRow as $sField => $sValue)
                {
                        // $sField is the field name
                        // $sValue is the field value
                }
        }
Paul

« previous php.general (#234019) next »