简体   繁体   中英

PHP - Does mysql_fetch_assoc return a two dimensional array?

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!

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).

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;

array_pop将取消设置数组的第一个值并返回它,因此在您的情况下,第一个表字段将被取消设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM