Re: [RFC] Add fetch_column method to mysqli
| From: | Nikita Popov | Date: | Wed, 14 Apr 2021 08:48:35 +0000 |
| Subject: | Re: [RFC] Add fetch_column method to mysqli | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-114050@lists.php.net to get a copy of this message | ||
On Sun, Mar 28, 2021 at 6:00 PM Kamil Tekiela <tekiela246@gmail.com> wrote:
> 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