Welcome to WebmasterWorld Guest from 107.20.5.156

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get an array from a SQL database column

     

moivreus

10:47 pm on Mar 17, 2010 (gmt 0)

5+ Year Member



Hi all, I'm new to php so I encounter problems which some of you might not find as such. For example:
I have a db with a table with only 2 columns: id & category, so the data flows as: "1, 2, 3..." and "category1, category2, category3..."
And I want to get an array that looks like: $array1 = array(category1, category2, category3...) , basically containing all the values from the "category" column. How do I do that with PHP and MYSQL?

gsmith

11:13 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Surely there must be more elegant ways of doing this, but you can store the values in a varchar field like this [1,2,3] (i.e. comma seperated), and execute code like this after reading that field:

eval( '$myArray = array(' . $row["myField"] . ');' );

Readie

12:00 am on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



This might be what the OP is after:

[uk2.php.net...]

moivreus

2:56 am on Mar 18, 2010 (gmt 0)

5+ Year Member



thanks Readie, I didn't understand why the result from mysql_fetch_row doesn't act as a proper array until I discovered that it should be flatten first.

to gsmith: thanks for the answer, but it's way too advanced for me

[edited by: moivreus at 3:01 am (utc) on Mar 18, 2010]