# mysqli_fetch() return values

- Thread: https://externals.io/message/7937
- Original archive: https://news-web.php.net/php.internals/7937
- Started by Adam Maccabee Trachtenberg on 2004-02-17T21:46:15+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #7937 — Adam Maccabee Trachtenberg — 2004-02-17T21:46:15+00:00

URL: https://externals.io/message/7937#7937

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!

---

## #7947 — Georg Richter — 2004-02-18T07:51:32+00:00

URL: https://externals.io/message/7937#7947 · In reply to: #7937

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

---

## #7948 — Adam Trachtenberg — 2004-02-18T08:29:32+00:00

URL: https://externals.io/message/7937#7948 · In reply to: #7947

> > […]
>
> 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
