成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? PHP:MySQL函數mysql_fetch_array()的用法
全站頭部文字 我要出現在這里
PHP:MySQL函數mysql_fetch_array()的用法 780 2023-12-13   

mysql_fetch_array

(PHP 4, PHP 5)

mysql_fetch_array — 從結果集中取得一行作為關聯數組,或數字數組,或二者兼有

說明

array mysql_fetch_array ( resource $result [, int $ result_type ] )

返回根據從結果集取得的行生成的數組,如果沒有更多行則返回 FALSE

mysql_fetch_array() 是 mysql_fetch_row() 的擴展版本。除了將數據以數字索引方式儲存在數組中之外,還可以將數據作為關聯索引儲存,用字段名作為鍵名。

如果結果中的兩個或以上的列具有相同字段名,最后一列將優先。要訪問同名的其它列,必須用該列的數字索引或給該列起個別名。對有別名的列,不能再用原來的列名訪問其內容(本例中的 'field')。

Example #1 相同字段名的查詢

select table1.field as foo, table2.field as bar from table1, table2

有一點很重要必須指出,用 mysql_fetch_array() 并不明顯 比用 mysql_fetch_row() 慢,而且還提供了明顯更多的值。

mysql_fetch_array() 中可選的第二個參數 result_type 是一個常量,可以接受以下值:MYSQL_ASSOC,MYSQL_NUM 和 MYSQL_BOTH。本特性是 PHP 3.0.7 起新加的。本參數的默認值是 MYSQL_BOTH。

如果用了 MYSQL_BOTH,將得到一個同時包含關聯和數字索引的數組。用 MYSQL_ASSOC 只得到關聯索引(如同 mysql_fetch_assoc() 那樣),用 MYSQL_NUM 只得到數字索引(如同 mysql_fetch_row() 那樣)。

Note: 此函數返回的字段名大小寫敏感。

Example #2 mysql_fetch_array 使用 MYSQL_NUM

<?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        printf ("ID: %s  Name: %s", $row[0], $row[1]);
    }

    mysql_free_result($result);
?>

Example #3 mysql_fetch_array 使用 MYSQL_ASSOC

<?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        printf ("ID: %s  Name: %s", $row["id"], $row["name"]);
    }

    mysql_free_result($result);
?>

Example #4 mysql_fetch_array 使用 MYSQL_BOTH

<?php
    mysql_connect("localhost", "mysql_user", "mysql_password") or
        die("Could not connect: " . mysql_error());
    mysql_select_db("mydb");

    $result = mysql_query("SELECT id, name FROM mytable");

    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
        printf ("ID: %s  Name: %s", $row[0], $row["name"]);
    }

    mysql_free_result($result);
?>

參見 mysql_fetch_row() 和 mysql_fetch_assoc()。

參數

result

resource 型的結果集。此結果集來自對 mysql_query() 的調用。

result_type

The type of array that is to be fetched. It's a constant and can take the following values: MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH.

返回值

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

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 must use the numeric index of the column or make an alias for the column. For aliased columns, you cannot access the contents with the original column name.

范例

Example #5 Query with aliased duplicate field names

SELECT table1.field AS foo, table2.field AS bar FROM table1, table2

Example #6 mysql_fetch_array() with MYSQL_NUM

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

mysql_free_result($result);
?>

Example #7 mysql_fetch_array() with MYSQL_ASSOC

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

mysql_free_result($result);
?>

Example #8 mysql_fetch_array() with MYSQL_BOTH

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
    printf ("ID: %s  Name: %s", $row[0], $row["name"]);
}

mysql_free_result($result);
?>

注釋

Note: Performance

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

Note: 此函數返回的字段名大小寫敏感。

Note: 此函數將 NULL 字段設置為 PHP NULL 值。

參見

mysql_fetch_row() - 從結果集中取得一行作為枚舉數組 mysql_fetch_assoc() - 從結果集中取得一行作為關聯數組 mysql_data_seek() - 移動內部結果的指針 mysql_query() - 發送一條 MySQL 查詢


主站蜘蛛池模板: 勇士之门 电影| 髋关节置换术后护理ppt| 幻想电影在线播放完整版| 各各他的爱的歌谱| 色黄视频免费观看| 李莉莉| 泰星mike| 竹内纱里奈兽皇番号| 李乃文宋丹丹朱媛媛演的电视剧| 土壤动植物的乐园教学反思| 日韩在线日韩| 董卿简历| 山东教育电视台直播| 文王一支笔的功效与作用| 废纸板拳击手| 幼儿识字入门100字| 三浦亚沙妃| 谭耀文演的电影| 免费看污视频在线观看| 数学二年级上册答案| 男人上路| 2013年9月份日历表| 美女网站视频免费| 好好说再见| 张俪eyely| 风平浪静电影| 草船借箭剧本| 远景山谷 (1981)中字| 好一个中国大舞台简谱| 阴阳界 电影| av午夜| 美腿丝袜高跟三级视频| 可爱美女跳舞蹈视频| 好妻子电视剧免费在线观看| 血色誓言演员表| 热带往事 电影| 安徽农金存款利率2024最新消息| 特殊的按摩| 黑水电影| 视频欧美| 秦时明月动画片|

!!!站長長期在線接!!!

網站、小程序:定制開發/二次開發/仿制開發等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103