Name

mysql_fetch_object

Synopsis

object mysql_fetch_object(qresource query [, int array_type])

Returns an object that contains the next available row from the result set associated with the parameter query. The internal pointer associated with the result set is then incremented, so that the next call to this function will retrieve the next row. The query resource handle is returned from a prior call to mysql_query( ) or mysql_unbuffered_query( ). The function returns false when no more rows are available.

Each row is returned as an object, and all member variables of the object are of type string. The second parameter array_type should be included and set to MYSQL_ASSOC. Numeric indexes cannot be used to access objects.

The same associative access limitations that apply to mysql_fetch_array( ) apply to mysql_fetch_object( ). There is one additional limitation: aggregate functions must be aliased for associative access, because parentheses and other special characters are invalid in member variable names. Thus, the sum( ) function in the statement SELECT sum(quantity) as total FROM presents can be accessed associatively in the object $row as $row->total.

Example

<?php $query = "SELECT * FROM presents"; $connection = mysql_connect("localhost", "fred", "shhh"); mysql_select_db("wedding", $connection); $result = mysql_query($query, $connection); while ($row = mysql_fetch_object($result, MYSQL_ASSOC)) { echo "\n\nQuantity:\t" . $row->quantity; echo "\nPresent:\t" . $row->present; echo ...

Get Managing & Using MySQL, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.