note 33864 added to class.com

From: Date: Wed, 09 Jul 2003 15:33:12 +0000
Subject: note 33864 added to class.com
Groups: php.notes 
Request: Send a blank email to php-notes+get-51794@lists.php.net to get a copy of this message
If you want to use a DSN-less connection to a database (access for example) and return the results of a query as a multidimensional array with the fieldnames as identifier try this. Hope it helps someone :-) -------------------------------------------------------- function db($sql) { $c = new COM("ADODB.Connection"); $c->open('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' . realpath("relative_path/db.mdb")); $r = $c->execute($sql); $i = 0; $num_fields = $r->fields->count(); while (!$r->EOF) { for($j = 0;$j<$num_fields;$j++) { $key = $r->Fields($j); $r_items[$i][$key->name] = $key->value; } $i++; $r->MoveNext(); } $r->close(); $c->close(); $r = null; $c = null; return $r_items; } -------------------------------------------------------- use it like this: -------------------------------------------------------- $results = db("SELECT field_a, field_b FROM table"); foreach($result as $i => $item) { echo $item['field_a']; echo $item['field_b']; } -------------------------------------------------------- You can also use: print_r($result); if you want the structure of the array to be printed. ---- Manual Page -- http://www.php.net/manual/en/class.com.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+33864 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+33864&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+33864&report=yes

« previous php.notes (#51794) next »