# [RFC] Add fetch_column method to mysqli

- Thread: https://externals.io/message/113832
- Original archive: https://news-web.php.net/php.internals/113832
- Started by Kamil Tekiela on 2021-03-28T15:59:50+00:00
- Messages: 4
- 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

---

## #113832 — Kamil Tekiela — 2021-03-28T15:59:50+00:00

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

Hi Internals,

I have written a new proposal in the series of improvements to mysqli
extension. This one aims to add a new method mysqli_result::fetch_column as
well as its functional counterpart.

https://wiki.php.net/rfc/mysqli_fetch_column

The RFC is very simple, but I am looking for your opinions. I also have
doubts about the scope. Is the scope too limited? Should it involve a
change to fetch_all as well?

On the implementation part, I wonder if it could be optimized. The one I
have written works but doesn't look like the most efficient way to do this.

Kind Regards,
Kamil Tekiela

---

## #113946 — Kamil Tekiela — 2021-04-04T23:17:20+00:00

URL: https://externals.io/message/113832#113946 · In reply to: #113832

I am bumping this thread. I assume that it got lost a little bit with was
going on over the past week.
I have implemented changes to fetch_all by adding two new constants and
another optional parameter. This is only MVP. Both changes can be seen
together here
https://github.com/php/php-src/compare/master...kamil-tekiela:mysqli-fetch_all-column

Is this something that people would want to see implemented in mysqli?
Should the RFC be amended to include fetch_all() as well?

---

## #114050 — Nikita Popov — 2021-04-14T08:48:35+00:00

URL: https://externals.io/message/113832#114050 · In reply to: #113832

> Hi Internals,
>
> I have written a new proposal in the series of improvements to mysqli
> extension. This one aims to add a new method mysqli_result::fetch_column as
> well as its functional counterpart.
>
> https://wiki.php.net/rfc/mysqli_fetch_column
>
> The RFC is very simple, but I am looking for your opinions. I also have
> doubts about the scope. Is the scope too limited? Should it involve a
> change to fetch_all as well?
>
> On the implementation part, I wonder if it could be optimized. The one I
> have written works but doesn't look like the most efficient way to do this.
>

Looks like a reasonable addition to me.

The implementation can be optimized if mysqlnd is used. It's possible to
call mysqlnd_fetch_row_zval(), which will return the row in a flat zval
buffer, and then pick out the one result you want and destroy the rest. You
can find a sample usage in ext/mysql:
https://github.com/php/pecl-database-mysql/blob/ca514c4dfacd7f1495d2fe142101fe3d91494d12/php_mysql.c#L2103-L2112

Not sure about fetch_all() support. I think the primary use case here is
the case where you expect exactly one result (or zero/one results) and just
need the one fetch_column() call. If you have multiple results you should
be iterating the result set, not calling fetch_all()...

Regards,
Nikita

---

## #114054 — Kamil Tekiela — 2021-04-14T13:55:30+00:00

URL: https://externals.io/message/113832#114054 · In reply to: #114050

Hi Nikita,

>The implementation can be optimized if mysqlnd is used.

Thanks for the suggestion. I tried it but I only see ~1% performance
improvement. I don't think it's worth it.

I see that there is not much interest, so I will keep the RFC simple and
without fetch_all() changes.

I will start voting on this RFC soon if there are no more comments.

Regards,
Kamil
