Greetings. There is a MySQL table with id and name columns, for example. What is the shortest way I can get an array of all values ​​from the name column?

Of course, I can go through the loop and with the help of mysqli_fetch_assoc put the value from a specific column from each row into an array, but are there any more elegant solutions?

  • Well, or mysqli_fetch_array. - Akina
  • @ Jean-Claude, and then select arrays by columns through array_column , right? Thank you, your advice helped, issue a response. - LNK

1 answer 1

  1. Write a SQL query, example SELECT id, name FROM tablename;

  2. Execute it and get the result set $result = $mysqli->query($query)

  3. Now from the result set you want to extract the data $array_data = $result->fetch_all() . Documentation

For PDO: PDOStatement :: fetchAll immediately forms an array of all the rows in the result sql query set http://php.net/manual/ru/pdostatement.fetchall.php

  1. As a result, you get an array of $array_data from pairs of values ​​id and name. Check it with var_damp.