[thelist] Re: array of results in PHP

kverens at contactjuggling.org kverens at contactjuggling.org
Mon Feb 24 12:31:09 CST 2003


Andrew Maynes writes:

> Hi Kae
>
> I having been reading up mysql_fetch_array and it appears I need to change my
> existing code to something like this:
>
> while ( $myrow = mysql_fetch_array($result) )
>
> printf("<table border=0 cellspacing=0 cellpadding=0><tr><td><p>Author :
> %s</td></tr>
> <tr><td><p>Message : %s</p></td></tr>
> <tr><td><p>Date : %s</p></td></tr></table><br><hr><br>", $myrow["name"],
> $myrow["message"], $myrow["time"]);
>
> }
>
> Am I right?  Is this why the query isn't working do you think?

The main reasons your code wasn't working was that you were connecting okay
(mysql_connect()), but were then querying and specifying an unassigned
connection (mysql_query($DBNAME,'select blah blah...') - where did $DBNAME
come from?).

It is better for future-proofing that you address your variables as $r['id']
rather than $r[0], but it's not a /must/ - the reasoning is simple - if at
some time the table layout changes and a new field is entered before
$r['id'] (for example), then all $r[0] (etc) addresses will be invalid. The
name based versions, though, will be fine.

In my opinion, though, printf() should only be used when you have actual
formatting to do to the values (printf('&euro;%.2f',234); for example)

Kae



More information about the thelist mailing list