Emkey
Emkey

Reputation: 5356

How can I make this array with data from a database?

How can I make this array with data from a database?

$array=array("a"=>"Apple","b"=>"Ball","c"=>"Cat");

I have a database table with column letter and value.

letter | value
       |
  a    | Apple
  b    | Ball
  c    | Cat

I want "a"=>"Apple","b"=>"Ball","c"=>"Cat" to be values from the database, using for loop, how is that possible?

Many thanks for any help!

Upvotes: 4

Views: 1842

Answers (1)

user557846
user557846

Reputation:

assuming you can do the connection and select

$array=array();
while ($row = mysql_fetch_assoc($result)) {
    $array[$row['letter']]=$row['value'];
}

print_r($array);

Upvotes: 10

Related Questions