0

I know there is mysql_fetch_assoc which puts all data from a row into an array. Is there something equivalent for putting all data in a column into an array? e.g. a list of title ID's?

3 Answers 3

1

GROUP_CONCAT and explode() is probably what you're looking for as it can get everything in one row. You can group all of the values with a comma using GROUP_CONCAT and then populate the array using explode().

Your SQL would contain this:

SELECT GROUP_CONCAT(column_name SEPARATOR ',') AS col

And your PHP would create the array like this (after performing the query):

$col_array = explode(',' $row['col']);
Sign up to request clarification or add additional context in comments.

Comments

0

Here's my suggestion:

$arr = array();
while ($row = mysql_fetch_assoc($result)) {
  $arr[] = $row['id']; // replace id with your column name.
}

var_dump($arr);

Comments

0

If you're able to use external libraries, it may be worth looking into the Zend database library. Zend_Db provides a function called fetchCol where it will return an array without having to write any additional code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.