Entry
Why use mysql_fetch_object when the object returned is not a member of any class and consequently has no methods?
Jan 6th, 2003 17:23
Jean-Marc Molina, Jamie Norrish,
mysql_fetch_object "Fetch a result row as an object" and "Returns an
object with properties that correspond to the fetched row, or FALSE if
there are no more rows" (PHP Manual).
The object doesn't contain any method, only properties. A PHP class or
object that doesn't contain any method is a structure. So a class or
object is a structure (properties) that has at least one method. But
you still can create an object with no property.
PHP uses the MySQL C API, not a C++ or object-oriented one. The MySQL C
API doesn't contain any mysql_fetch_object or mysql_fetch_array
function. These functions are PHP specific. Objects don't exist in
MySQL, it's a PHP feature.
That's why the object that mysql_fetch_object returns is not really an
object, because the PHP-MySQL API is only made up by functions.
But some people use PHP as an object-oriented language, they only want
to deal with objects and classes, not arrays (mysql_fetch_array), so
they don't use mysql_fetch_row or mysql_fetch_array. But I think they
should use PEAR (http://pear.php.net) or an other MySQL-Database object-
oriented wrapper.
I don't use mysql_fetch_object because I think it's an other useless
PHP feature, the MySQL API is a set of functions, their arguments are
arrays or simple variables, so let's use the MySQL C API functions (the
MySQL official standard not the PHP-MySQL one):
http://www.mysql.com/doc/en/C.html
note: mysql_fetch_array is not a MySQL C API function but it's very
useful and powerful