rfc:mysqli_fetch_column

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:mysqli_fetch_column [2021/03/28 15:25] – created dharmanrfc:mysqli_fetch_column [2021/06/11 11:53] (current) – Implemented dharman
Line 5: Line 5:
   * Target version: PHP 8.1   * Target version: PHP 8.1
   * Implementation: https://github.com/php/php-src/pull/6798   * Implementation: https://github.com/php/php-src/pull/6798
-  * Status: Discussion+  * Status: Implemented (PHP 8.1)
  
  
Line 12: Line 12:
  
 ===== Proposal ===== ===== Proposal =====
-As of now, there is no easy way to fetch a scalar value in mysqli. If the SQL statement returns no rows then the simplest way to fetch scalars is to fetch an array and using the null-coalescing operator default the value to false. +As of now, there is no easy way to fetch a scalar value in mysqli. If the SQL statement can return no rows then the simplest way to fetch scalars is to fetch an array and using the null-coalescing operator default the value to false. This is due to all the fetch methods returning false if no more rows are present in the result set.
 <PHP> <PHP>
 $result = $mysqli->query('SELECT username FROM users WHERE id = 123'); $result = $mysqli->query('SELECT username FROM users WHERE id = 123');
Line 51: Line 51:
  
 ===== Future Scope ===== ===== Future Scope =====
-It would be nice to have similar functionality available with %%mysqli_result::fetch_all()%%. This is not included in the current proposal due to the fact that this change would involve adding a new optional parameter, which would have to be an integer or a string. The parameter couldn't be limited in scope to only column index, but the method would have to be extended to handle classes with the MYSQLI_CLASS constant as well. This is certainly possible but would be much more work. +It would be nice to have similar functionality available with %%mysqli_result::fetch_all()%%. This is not included in the current proposal due to the fact that it would require rewriting that method to support the MYSQLI_CLASS constant as well. This is certainly possible but would be much more work.  
 + 
 +Of course, it is very simple to fetch single column values into an array with the current functionality: 
 +<PHP> 
 +$result = $mysqli->query('SELECT username FROM users'); 
 +$usernames = []; 
 +foreach ($result as ['username' => $usernames[]]); 
 +</PHP>
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
 Simple yes/no vote. Simple yes/no vote.
  
 +Voting started on 2021-04-17 17:00:00Z and ends on 2021-05-01 17:00:00Z
  
 +<doodle title="Add mysqli_fetch_column to mysqli?" auth="Dharman" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
-===== Patches and Tests ===== 
-The implementation is available at https://github.com/php/php-src/pull/6798 
  
 ===== Implementation ===== ===== Implementation =====
-After the project is implemented, this section should contain  +[[https://github.com/php/php-src/pull/6798|PR here]]
-  the version(s) it was merged into +
-  - a link to the git commit(s) +
-  - a link to the PHP manual entry for the feature +
-  - a link to the language specification section (if any)+
  
 ===== References ===== ===== References =====
  
 +https://externals.io/message/113832#113832
rfc/mysqli_fetch_column.1616945135.txt.gz · Last modified: 2021/03/28 15:25 by dharman