I am doing some work that requires me to add the data from a specific column, say Column 1, to a PHP array. I can get the data from the first row in Column 1, but it stop there. How do I collect that columns data from every row in the table?
3
-
SELECT RowName FROM TableStefan H– Stefan H2010-12-20 01:09:02 +00:00Commented Dec 20, 2010 at 1:09
-
Ok @Stefan H... How do I display this data?Zac Brown– Zac Brown2010-12-20 01:10:48 +00:00Commented Dec 20, 2010 at 1:10
-
Display as in print the html?Stefan H– Stefan H2010-12-20 01:14:09 +00:00Commented Dec 20, 2010 at 1:14
Add a comment
|
2 Answers
You have to loop over the result set in a while loop:
$result = mysql_query('SELECT...');
$data = array();
while(($row = mysql_fetch_array($result))) {
$data[] = $row['columnName'];
}
Every call to mysql_fetch_array will get the next row of the result set. If there is no row anymore, it will return null and the loop stops.
The documentation provides good examples.
Update:
Regarding duplicates: Either specify your SQL query correctly (preferred), e.g.
SELECT DISTINCT columnName FROM table
or use array_unique after you fetched all the data:
$data = array_unique($data);
1 Comment
Zac Brown
Awesome! Thanks, you wouldn't happen to know how I would remove duplicates from that array? Would you?