Organizational Research By

Surprising Reserch Topic

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

mysql_fetch_array


PHP function to store those values in an array.

Any ideas on this?

Thanks in advance!
    

asked Sep 14, 2015 by SalleyWengeri
0 votes
6 views



Related Hot Questions

6 Answers

0 votes

This would work, see more documentation here : http://php.net/manual/en/function.mysql-fetch-array.php

$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 14, 2015 by CorGardner
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 14, 2015 by DebraEusebio
0 votes
$names=array();
$q="select names from Customers";
$rs=mysql_query($q);
while($rd=mysql_fetch_object($rs))
{
$names[]=$rd->names;
}

You can check your array now

print_r($names);
answered Sep 14, 2015 by MaybelleBidm
0 votes
$query = 'Select names FROM Customers';
$result = mysql_query($query);
$names = array();
while($r = mysql_fetch_array($result))
{
    array[] = $r["names"];
}

After this step, you have all names in the $names array.

answered Sep 14, 2015 by ScarlettReyn
0 votes
$result = mysql_query("SELECT names FROM Customers");

$myArray = array();

while ($row = mysql_fetch_assoc($result)) {
   $myArray[] = $row['names'];  
}

print_r($myArray);
answered Sep 14, 2015 by MarciaYgvwgu
0 votes
    $resultInsert = mysql_query("SHOW COLUMNS FROM " . $table);

    $fieldnames=array();

      if (mysql_num_rows($resultInsert) > 0) {
        while ($row = mysql_fetch_array($resultInsert)) {
            $fieldnames[] = $row['Field'];
    }
answered Sep 14, 2015 by Bre9972

...