After this question, what is the best way to get all the values of a single column in a MySQL database?
For instance, all values of:
SELECT Name FROM Customers
Looking for a non-deprecated answer.
After this question, what is the best way to get all the values of a single column in a MySQL database?
For instance, all values of:
SELECT Name FROM Customers
Looking for a non-deprecated answer.
Here is a simple way to do this using PDO
$stmt = $pdo->prepare("SELECT Column FROM foo LIMIT 5");
$stmt->execute();
$array = $stmt->fetchAll(PDO::FETCH_UNIQUE | PDO::FETCH_GROUP);
var_dump(array_keys($array));
array(5) {
[0]=>
int(7960)
[1]=>
int(7972)
[2]=>
int(8028)
[3]=>
int(8082)
[4]=>
int(8233)
}
$link = mysqli_connect($host, $user, $password) or die('DB Server Connection error'.mysqli_error());
mysqli_select_db($link, $database) or die('DB Selection error'.mysqli_error());
$sql = 'SELECT name FROM Customers';
$query = mysqli_query($link, $sql);
if (0 < mysqli_num_rows($query)) {
while ($data = mysqli_fetch_assoc($query)) {
$resultArray[] = $data['name'];
}
}