mysql_fetch_assoc

(PHP 4 >= 4.0.3, PHP 5)

mysql_fetch_assoc 锟接斤拷锟斤拷锟斤拷锟饺★拷锟揭伙拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷锟�

Warning

锟斤拷锟斤拷展锟斤拷 PHP 5.5.0 锟斤拷锟窖凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP 7.0.0 锟斤拷始锟斤拷锟狡筹拷锟斤拷应使锟斤拷 MySQLi 锟斤拷 PDO_MySQL 锟斤拷展锟斤拷锟芥换之锟斤拷锟轿硷拷 MySQL锟斤拷选锟斤拷 API 指锟斤拷锟皆硷拷锟斤拷锟� FAQ 锟斤拷锟斤拷取锟斤拷锟斤拷锟斤拷息锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷校锟�

说锟斤拷

mysql_fetch_assoc ( resource $result ) : array

锟斤拷锟截讹拷应锟斤拷锟斤拷锟斤拷墓锟斤拷锟斤拷锟斤拷椋拷锟斤拷壹锟斤拷锟斤拷贫锟斤拷诓锟斤拷锟斤拷锟街革拷搿� mysql_fetch_assoc() 锟斤拷锟斤拷 mysql_fetch_array() 锟斤拷锟较第讹拷锟斤拷锟斤拷选锟斤拷锟斤拷 MYSQL_ASSOC 锟斤拷全锟斤拷同锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟截癸拷锟斤拷锟斤拷锟介。

锟斤拷锟斤拷

result

resource 锟酵的斤拷锟斤拷锟斤拷锟斤拷私锟斤拷锟斤拷锟斤拷锟皆讹拷 mysql_query() 锟侥碉拷锟矫★拷

锟斤拷锟斤拷值

锟斤拷锟截革拷锟捷从斤拷锟斤拷锟饺★拷玫锟斤拷锟斤拷锟斤拷傻墓锟斤拷锟斤拷锟斤拷椋伙拷锟斤拷没锟叫革拷锟斤拷锟斤拷锟津返伙拷 FALSE锟斤拷

锟斤拷锟斤拷锟斤拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟较碉拷锟叫撅拷锟斤拷锟斤拷同锟街讹拷锟斤拷锟斤拷锟斤拷锟揭伙拷薪锟斤拷锟斤拷取锟揭拷锟斤拷锟酵拷锟斤拷锟斤拷锟斤拷锟斤拷校锟揭达拷锟� mysql_fetch_row() 锟斤拷取锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 锟轿硷拷 mysql_fetch_array() 锟斤拷锟斤拷锟斤拷锟叫关憋拷锟斤拷说锟斤拷锟斤拷

锟斤拷锟斤拷

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

?>

注锟斤拷

Note: 锟斤拷锟斤拷

锟斤拷锟斤拷指锟斤拷一锟斤拷要锟姐: mysql_fetch_assoc() 锟斤拷 mysql_fetch_row() 锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟揭伙拷锟结供锟剿革拷锟斤拷锟斤拷锟矫碉拷值锟斤拷

Note: 锟剿猴拷锟斤拷锟斤拷锟截碉拷锟街讹拷锟斤拷锟斤拷小写锟斤拷锟斤拷锟斤拷

Note: 锟剿猴拷锟斤拷锟斤拷 NULL 锟街讹拷锟斤拷锟斤拷为 PHP NULL 值锟斤拷

锟轿硷拷

  • mysql_fetch_row() - 锟接斤拷锟斤拷锟斤拷锟饺★拷锟揭伙拷锟斤拷锟轿讹拷锟斤拷锟斤拷锟�
  • mysql_fetch_array() - 锟接斤拷锟斤拷锟斤拷锟饺★拷锟揭伙拷锟斤拷锟轿拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷锟斤拷锟斤拷锟斤拷椋拷锟斤拷锟竭硷拷锟斤拷
  • mysql_data_seek() - 锟狡讹拷锟节诧拷锟斤拷锟斤拷锟街革拷锟�
  • mysql_query() - 锟斤拷锟斤拷一锟斤拷 MySQL 锟斤拷询
  • mysql_error() - 锟斤拷锟斤拷锟斤拷一锟斤拷 MySQL 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟侥憋拷锟斤拷锟斤拷锟斤拷息