homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Get an array from a SQL database column

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

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?



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

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"] . ');' );


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

This might be what the OP is after:



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

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]

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved