php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #14091 Problem with main example on the "MySQL Functions" page
Submitted: 2001-11-17 06:07 UTC Modified: 2001-11-17 06:34 UTC
From: blakjak at webone dot com dot au Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.6 OS: RHL 6.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: blakjak at webone dot com dot au
New email:
PHP Version: OS:

 

 [2001-11-17 06:07 UTC] blakjak at webone dot com dot au
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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-17 06:34 UTC] goba@php.net
You are right, mysql_fetch_array() uses MYSQL_BOTH by default. I have just corrected the example, and
added some comments, so it works OK...

Do not expect it to show up in a day, as the HTML
generation from XML is done daily.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Sep 17 03:01:27 2024 UTC