mysqli_fetch() return values

php.internals

Adam Maccabee Trachtenberg

22 years ago
I would like to propose a change in how mysqli_fetch() signifies "no more data." It should return NULL instead of MYSQLI_NO_DATA (a positive value) because it leads to cleaner PHP code and is more consistent with other MySQL fetch functions. Currently, mysqli_fetch() returns one of three different values: Value | Meaning --------------------------------- true | Successful Fetch false | Error MYSQLI_NO_DATA | No more data --------------------------------- Returning the constant MYSQLI_NO_DATA (100) mimics the underlying MySQL C API, but it leads to non-natural PHP Code: while (MYSQLI_NO_DATA !== mysqli_fetch($db)) { // do something. } Every other MySQL function allows you to do: while ($row = mysqli_fetch_row($db)) { // do something. } And I think it'd be much nicer to allow people to write: while (mysqli_fetch($db)) { // do something. } Therefore, I propose that instead of returning MYSQLI_NO_DATA, we return NULL. Here's my reasoning: 1) The other mysqli_fetch_* functions now return NULL in this situation. 2) We're already modifying the MySQL C-level return values. In C, mysql_fetch() returns 0 on success and 1 upon an error. We've swapped these values to behave more PHP-like. 3) When 0 signifies success, a positive "no more data" error code makes sense because it looks (logically) like an error inside a while. That doesn't hold true now that we've flipped these values. -adam
-- adam@trachtenberg.com author of o'reilly's php cookbook avoid the holiday rush, buy your copy today!

Georg Richter

22 years ago
Hi Adam,
> I would like to propose a change in how mysqli_fetch() signifies "no > more data." It should return NULL instead of MYSQLI_NO_DATA (a > positive value) because it leads to cleaner PHP code and is more > consistent with other MySQL fetch functions.
Thanks for pointing it out. The reason was, that we weren't sure if we need other return values (array fetch, etc) for MySQL 5.0/5.1 - but we will add a separate function for. I will change it and update documentation later today. Georg

Adam Maccabee Trachtenberg

22 years ago
On Feb 18, 2004, at 2:51 AM, Georg Richter wrote:
>> I would like to propose a change in how mysqli_fetch() signifies "no >> more data." It should return NULL instead of MYSQLI_NO_DATA (a >> positive value) because it leads to cleaner PHP code and is more >> consistent with other MySQL fetch functions. > > Thanks for pointing it out. The reason was, that we weren't sure if we > need > other return values (array fetch, etc) for MySQL 5.0/5.1 - but we will > add a > separate function for. I will change it and update documentation later > today.
Great. Thanks. -adam
-- adam trachtenberg adam@trachtenberg.com