Daniel Hunter
Daniel Hunter

Reputation: 2876

display all cells in a mysql column

I need all values from a table column put into an assoc array. I am having trouble finding the answer.

the table is only six rows deep. example:

|--id--|--name--|--A--|--B--|--C--|
|--01--|--xl33--| 1.30| 2.45| 4.40|

i would like to get an assoc array for column B

name[xl33]=2.45
name[xl34]=....and so on

The trick is that the form will tell the script which column to fetch. A,B,C,D,E,F OR G

I know i could re-format the table and accomplish what i want but I need it structured the way i have it.( i have left out some columns for simplicity)

Upvotes: 1

Views: 132

Answers (4)

472084
472084

Reputation: 17895

function return_column($letter){
    $result = mysql_query("SELECT name, $letter FROM example") or die(mysql_error());
    while($row = mysql_fetch_assoc( $result )) {
        $return[$row['name']] = $row[$letter];
    }
    return $return;
}

$results = return_column('A');
print_r($results);

Upvotes: 1

user319198
user319198

Reputation:

Use mysql_fetch_assoc — Fetch a result row as an associative array.

while ($row = mysql_fetch_assoc($result)) {
         $data[$row["column1"]]= $row["column2"];

        ..............
        .........
    }

Upvotes: 0

Matt K
Matt K

Reputation: 6708

You are looking for the mysql_fetch_assoc() function.

Example/

$query = $this->query($YOUR_QUERY);
$returnMap = array();
while ($row = mysql_fetch_assoc($query)) { array_push($returnMap, $row); }

Upvotes: 0

Manse
Manse

Reputation: 38147

Something like :

$col = 'B';
$name = array();
$result = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
    $name[$row['name']] = $row[$col];

}

This creates an array $name and uses the name column as the key and the $col column for the value ...

Upvotes: 1

Related Questions