0

i have a table (actual column names)

id | name | image1  | image2  | image3  | image4  | image5  | image6  | image7 etc
2    joe    1.jpg     2.jpg     3.jpg   etc

all i simply need to do is select just the entries in image(x) columns & name (there are other column names buti dont want them) so i can display

For example:

select name, image(x) from table where id=2

this should produce an array so i can run through the loop and end up with

name
1.jpg
2.jpg
3.jpg

etc wich i could then echo out? - this must a pretty standard(ish) type query ?

i assuming that i have to assign image with image[0] or something in the loop ?

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
adeibiza
  • 23
  • 5
  • Did you try writing this code? – Sergio Tulentsev Dec 29 '11 at 15:33
  • It appears that you want to pivot your columns and make them rows. Read the answers at this question: http://stackoverflow.com/questions/6605604/mysql-pivot-query-results-with-group-by – DwB Dec 29 '11 at 15:49

1 Answers1

1

This is not the way I would structure this table. You should just use id | name | image and have multiple records for each name. Then you can just do a

SELECT image FROM table WHERE name = 'joe'

This way you can pull back and build an array like the one you want easily without the kludgy (x) stuff.

Wes Crow
  • 2,971
  • 20
  • 24
  • if so, I would rather use 3 or 2 tables: user(id,name), imageUser(user_id,image) and if there is image info - image table. – lvil Dec 29 '11 at 15:46
  • This depends on the level of complexity of the system. Normalization is good, but if this is the only table that will ever exist in the database there is no need for it IMO. – Wes Crow Dec 29 '11 at 15:50
  • i dont have a choice in structure of the table i'm afraid - thats what ive been given to work with – adeibiza Dec 29 '11 at 16:28