1

when

$query = 'SELECT * FROM users';

and there are multiple columns/rows, does mysql_fetch_assoc($result) return a two-dimensional array?

Can I draw each row by simply saying: array_pop(mysql_fetch_assoc($r))

thanks!

3 Answers 3

6

mysql_fetch_assoc returns an associative array (an array with the selected column names as keys), one row at a time until there are no more rows in the result of the query. Call it in a loop to work with a row at a time:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["username"];
    echo $row["email"];
}

(Assuming username and email are columns in the users table).

Sign up to request clarification or add additional context in comments.

Comments

4

No, it returns a single row.

Calling it again will return the next row, until there are no more rows.

You can build a multi-dimensional array like this:

$rows = array();
while ($row = mysql_fetch_assoc($data))
    $rows[] = $row;

Comments

2

array_pop will unset the first value of the array and return it, so in your case your first table field would be unset.

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.