MySQL 函数
PHP Manual

mysql_fetch_assoc

(PHP 4 >= 4.0.3, PHP 5)

mysql_fetch_assoc 从结果集中取得一行作为关联数组

说明

array mysql_fetch_assoc ( resource $result )

返回根据从结果集取得的行生成的关联数组,如果没有更多行则返回 FALSE

mysql_fetch_assoc() 和用 mysql_fetch_array() 加上第二个可选参数 MYSQL_ASSOC 完全相同。它仅仅返回关联数组。这也是 mysql_fetch_array() 起初始的工作方式。如果在关联索引之外还需要数字索引,用 mysql_fetch_array()

如果结果中的两个或以上的列具有相同字段名,最后一列将优先。要访问同名的其它列,要么用 mysql_fetch_row() 来取得数字索引或给该列起个别名。参见 mysql_fetch_array() 例子中有关别名说明。

有一点很重要必须指出,用 mysql_fetch_assoc()不明显 比用 mysql_fetch_row() 慢,而且还提供了明显更多的值。

Note: 此函数返回的字段名大小写敏感

Example #1 扩展的 mysql_fetch_assoc() 例子

<?php

    $conn 
mysql_connect("localhost""mysql_user""mysql_password");

    if (!
$conn) {
        echo 
"Unable to connect to DB: " mysql_error();
        exit;
    }

    if (!
mysql_select_db("mydbname")) {
        echo 
"Unable to select mydbname: " mysql_error();
        exit;
    }

    
$sql "SELECT id as userid, fullname, userstatus
            FROM   sometable
            WHERE  userstatus = 1"
;

    
$result mysql_query($sql);

    if (!
$result) {
        echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
        exit;
    }

    if (
mysql_num_rows($result) == 0) {
        echo 
"No rows found, nothing to print so am exiting";
        exit;
    }

    
// While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //       then create $userid, $fullname, and $userstatus
    
while ($row mysql_fetch_assoc($result)) {
        echo 
$row["userid"];
        echo 
$row["fullname"];
        echo 
$row["userstatus"];
    }

    
mysql_free_result($result);

?>

参见 mysql_fetch_row()mysql_fetch_array()mysql_query()mysql_error()

参数

result

resource 型的结果集。此结果集来自对 mysql_query() 的调用。

返回值

Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysql_fetch_row() or add alias names. See the example at the mysql_fetch_array() description about aliases.

范例

Example #2 An expanded mysql_fetch_assoc() example

<?php

$conn 
mysql_connect("localhost""mysql_user""mysql_password");

if (!
$conn) {
    echo 
"Unable to connect to DB: " mysql_error();
    exit;
}
  
if (!
mysql_select_db("mydbname")) {
    echo 
"Unable to select mydbname: " mysql_error();
    exit;
}

$sql "SELECT id as userid, fullname, userstatus 
        FROM   sometable
        WHERE  userstatus = 1"
;

$result mysql_query($sql);

if (!
$result) {
    echo 
"Could not successfully run query ($sql) from DB: " mysql_error();
    exit;
}

if (
mysql_num_rows($result) == 0) {
    echo 
"No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row mysql_fetch_assoc($result)) {
    echo 
$row["userid"];
    echo 
$row["fullname"];
    echo 
$row["userstatus"];
}

mysql_free_result($result);

?>

注释

Note: Performance

An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

Note: 此函数返回的字段名大小写敏感

Note: 此函数将 NULL 字段设置为 PHP NULL 值。

参见


MySQL 函数
PHP Manual