How to Iterate over MySQLi Results Efficiently
You're encountering difficulty iterating through a MySQLi result set. Your current loop only returns the first value because of an issue with your fetch method.
The problem lies in using fetch_array() with the default setting of MYSQLI_BOTH, which generates a result set with both indexed and associative keys. To fix this, you can specify MYSQLI_ASSOC or MYSQLI_NUM to explicitly define the desired key type.
Alternatively, avoid using fetch_array() altogether and leverage the iterability of query results in MySQLi. Here's a more efficient approach:
foreach ($output as $row) { echo $row['uid']; }
This loop eliminates the need for fetch_array() and directly accesses the row's associated key.
However, even with fetch_array(), you must ensure that your iteration aligns with the actual structure of your result set. In your case, each result row is expected to have a single uid column, but your iteration assumes a different structure.
By adopting the more efficient iterable approach, you can effectively iterate over your MySQLi result sets.
The above is the detailed content of How to Efficiently Iterate Over MySQLi Results?. For more information, please follow other related articles on the PHP Chinese website!