Forum Moderators: coopster

Message Too Old, No Replies

fetching arrays php and mysql

         

ukgimp

3:16 pm on Oct 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is Friday and what I know is a simple task, is baffling me.

I have a function that uses and array in the following format.

$cat = array("1"=>"All","2"=>"red","3"=>"yellow","4"=>"green");

I have those recpective values in a db and can get them out easily with a query.

$sql = mysql_query("SELECT id, colour FROM category_two");
$test = mysql_fetch_array($sql, mysql_ASSOC);

But converting them into the above format is not hapening. All I get is one value. Can I not just grab the whole array in one go or do I need to build it up with some sort of while statement?

It has always confused me this array stuff :(

Does the above make sense.

coopster

3:42 pm on Oct 17, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Correct, you need to loop to get the values loaded into your array:

$sql = mysql_query("SELECT id, colour FROM category_two");
while ($test = mysql_fetch_array($sql, mysql_ASSOC)) {
$cat[] = $row['colour'];
}

ukgimp

3:45 pm on Oct 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Coop'

At least I am not mad. I just needed to clear that up. Probably saved me endless messing around.

Cheers

ukgimp

2:09 pm on Oct 20, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I was dealing with a multi dimentional array so here is the solution that I got working

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$key = $row[0];
$value = $row[1];
$myarray[$key]="$value";
}