Retrieving a Single Column into a One-Dimensional Array using PDO
Situation:
You wish to retrieve a specific column from a database table and populate it into a single-dimensional array.
Solution:
Directly query the database using PDO's query() method:
$sql = "SELECT `ingredient_name` FROM `ingredients`";
To retrieve a single column as an array, use fetchAll() with the PDO::FETCH_COLUMN parameter:
$ingredients = $pdo->query($sql)->fetchAll(PDO::FETCH_COLUMN);
This will result in an array containing only the ingredient_name column values.
Additional Tips:
The above is the detailed content of How Can I Retrieve a Single Column from a Database into a One-Dimensional Array Using PDO?. For more information, please follow other related articles on the PHP Chinese website!