note 108969 deleted from function.mysql-fetch-array by danbrown

From: Date: Fri, 08 Jun 2012 20:17:15 +0000
Subject: note 108969 deleted from function.mysql-fetch-array by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188488@lists.php.net to get a copy of this message
Note Submitter: Ivan Stojmenovic ---- Very useful for example. Database.class.php <?php /** *--------------------------------------------------------------- * RESULT ARRAY *--------------------------------------------------------------- * * This function returns the query result as a pure array, * or an empty array when no result is produced. * Typically you'll use this in a foreach for looping. * */ public function result_array($result = '', $type = 'MYSQL_BOTH') { $this->data = array(); if ( $type == 'MYSQL_BOTH') { while ($row = mysql_fetch_array($result, MYSQL_BOTH)) { $this->data[] = $row; } } return $this->data; } ?> How to use this method. User.class.php <?php $db = new Database; $query = $db->query('SELECT * FROM users'); // Fetch all users foreach($db->result($query) as $user) { echo $user['id']; echo $user['username']; } ?>

« previous php.notes (#188488) next »