When to use $sth->fetchrow_hashref, $sth->fetchrow_arrayref and $sth->fetchrow_array?

前端 未结 5 1933
不知归路
不知归路 2021-02-08 02:04

I know that:

  • $sth->fetchrow_hashref returns a hashref of the fetched row from database,
  • $sth->fetchrow_arrayref returns an
5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-08 02:49

    DBI has to do more work to present the result as a hashref than it does as an arrayref or as an array. If the utmost in efficiency is an issue, you will more likely use the arrayref or array. Whether this is really measurable is perhaps more debatable.

    There might be an even more marginal performance difference between the array and the arrayref.

    If you will find it easier to refer to the columns by name, then use the hashref; if using numbers is OK, then either of the array notations is fine.

    If the first thing you're going to do is return the value from the fetching function, or pass it onto some other function, then the references may be more sensible.

    Overall, there isn't any strong reason to use one over the other. The gotcha highlighted by Ed Guiness can be decisive if you are not in charge of the SQL.

提交回复
热议问题