Wednesday, April 25, 2007

PHP: mysql_fetch_row vs mysql_fetch_object

This is probably another one that everybody and their brother knew except me. I've always used mysql_fetch_row() to parse thru SQL results in PHP, but recently discovered mysql_fetch_object().

With mysql_fetch_row, you get an array of the items in the row... meaning if you queried the database with 'SELECT id, name, date FROM table', and call $row = mysql_fetch_row($query), then $row[0] is the id, $row[1] is the name, and $row[2] is the date. Seems pretty easy, until i found mysql_fetch_object.

Using that command, those same array vals are now object vals, so you can reference $row->id, $row->name, and $row->date. A little more typing, a lot easier to read. I think I'll stick with it.

No comments: