RE: [PHP-DB] Problem with foreach looping...

From: Date: Thu, 07 Apr 2005 19:19:17 +0000
Subject: RE: [PHP-DB] Problem with foreach looping...
Groups: php.db 
Request: Send a blank email to php-db+get-39151@lists.php.net to get a copy of this message
From: NIPP, SCOTT V (SBCSI) [mailto:sn4265@sbc.com] > I am attempting to use a simple foreach loop on a query result, > and I am only getting the first element of the array back. > Here is the > code... > > $exclude_query = "SELECT hostname FROM exclusion"; > $exclude_results = mysql_query($exclude_query, $Prod); > $exclude = mysql_fetch_array($exclude_results, MYSQL_NUM); > > foreach ($exclude as $row) { > echo "$row\n"; > } > > I don't understand how I am just getting a single item back from > this. The query actually returns all 13 entries in the > exclusion table. > I have tested the query via the mysql client. I also get all 13 > elements back if I loop via the following. > > while ($test = mysql_fetch_array($results, MYSQL_NUM)) { > echo "$test[0]\n"; > } > > Is my PHP somehow broke, or am I just missing something here? > Thanks. mysql_fetch_array() only fetches one record of the result set at a time. Thus, the code's doing what you told it to. The while() loop is what you want. Why do you need to use a foreach() loop? At that point, there's nothing to foreach() through. No array has been constructed. HTH! -- Mike Johnson Smarter Living, Inc. Web Developer www.smartertravel.com mike@smarterliving.com (617) 886-5539

« previous php.db (#39151) next »