Page 1 of 1

Query Results to Array [Solved]

Posted: Thu Feb 17, 2011 7:10 pm
by christh
Hello

I'm trying to modify a bit of php I've found, but either because it's the early hours of the morning here or I'm stupid, but I just can't get it to work - None of the examples I've found on the internet work for me either.

Basically the code I want to modify has the line

Code: Select all

$items = array(
"Car"=>"Red",
"Bike"=>"Blue");
Now, all I want to do is replace the static array with the results from my query - I thought...

Code: Select all

$result = mysql_query ("SELECT item, color FROM things");
	$items = array();
	while ($row = mysql_fetch_assoc($result)) {
	$items[] = $row;
	}
...would do the job but I'm wrong.

Can anybody help a tired amateur?

Many thanks

Chris

Re: Query Results to Array

Posted: Thu Feb 17, 2011 7:45 pm
by danwguy
I would think you would have to do something more along the lines of...

Code: Select all

$items = array('$row[item']', $row[seconditem'], '$row[thirditem'], //and so on...);
either that or it's the end of the day at work and I've been so bored that I am making absolutely no sense. Not sure at this point.

Re: Query Results to Array

Posted: Thu Feb 17, 2011 9:41 pm
by Jonah Bron

Code: Select all

$result = mysql_query ("SELECT item, color FROM things");
$items = array();
while ($row = mysql_fetch_assoc($result)) {
    $items[$row['item']] = $row['color'];
}
That ought to do the trick.

Re: Query Results to Array

Posted: Fri Feb 18, 2011 12:46 am
by amargharat

Code: Select all

$result = mysql_query ("SELECT item, color FROM things");
        $items = array();
        while ($row = mysql_fetch_assoc($result)) {
        $items[$row["item"]] = $row["color"];
        }

Re: Query Results to Array

Posted: Mon Feb 21, 2011 3:13 am
by christh
Hi Guys

Many thanks for the suggestions, which worked perfectly.

Cheers

Chris