how get all values in a column using php

how get all values in a column using php  using -'php,mysql,arrays'

I've been searching for this everywhere, but still can't find a solution: How do I get all the values from a mySQL column and store them in an array?

For eg:
 Table Name: Customers
 Column names: ID, Name
 # of rows: 5

I want to get an array of all the 5 names in this table. How do I go about doing that? I am using PHP, and I was trying to just:

SELECT names FROM Customers

and then use the


PHP function to store those values in an array.

Any ideas on this?

Thanks in advance!

asked Sep 15, 2015 by ClydeLeidig
0 votes

2 Answers

0 votes

This would work, see more documentation here :

$result = mysql_query("SELECT names FROM Customers");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[] =  $row['names'];  
// now $storeArray will have all the names.
answered Sep 15, 2015 by Maticaxx
0 votes

Hey thanks for the question, it helped me with my problem. I needed a function to check a column of names against the input. Here it is:

$query = "SELECT name FROM courses";
$result = mysql_query($query);
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $storeArray[] =  $row['name'];

foreach ($storeArray as $courseNameDB){
    if(strcmp($courseName, $courseNameDB) == 0){
        echo "false";
//outputs false when they are different
answered Sep 15, 2015 by MikU25