|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2001-11-17 06:34 UTC] goba@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 05:00:01 2025 UTC |
On the "LV. MySQL Functions" page of the PHP Manual, there is a piece of example code, demonstrating how to connect to MySQL, send a query and output the result of that query. The part I'm concerned about goes like this: print "<table>\n"; while ($line = mysql_fetch_array($result)) { print "\t<tr>\n"; while(list($col_name, $col_value) = each($line)) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; When I reproduced this example, something went wrong. Specifically, every value was printed twice. I'll elaborate on that - If my source table looked like this: id name --------- 1 blah 2 foo 3 bar Then the above code produced: 1 1 blah blah 2 2 foo foo 3 3 bar bar My guess is that, because mysql_fetch_array supplies BOTH associative and numerical indexes, using the "while(list = each)" format references two key/value pairs for every one. When I used mysql_fetch_assoc instead of mysql_fetch_array, the example worked perfectly. I assume that it would also work if you specified MYSQL_ASSOC or MYSQL_NUM for mysql_fetch_arry, but I haven't tested it. Feel free to email me if you need more detailed information. Sincerely, BJ