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

導航首頁 ? 技術教程 ? 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 查詢


主站蜘蛛池模板: 拿枪| 色戒.| 闺蜜心窍 电影| 奶粉罐回收多少钱一个| 我爱我爹全集高清版免费观看| 头文字d里演员表| 姐妹兄弟演员表| 永远的紫荆花简谱| 欧美吻戏视频| 乔治克鲁尼个人资料| 近郊| yy五项滚刀骂人套词| 爱我中华广场舞| 儿童视力| 《狂赌之渊》动漫| 蓝家宝电影| 父子激情视频| 雨后的故事34张原版视频| 考死2:教学实习| 女同恋性吃奶舌吻完整版| 左航个人资料| a型血和b型血生的孩子是什么血型| 南极之恋演员表| 掐脖子的视频| 欺辱尤娜| 电影《村小的孩子》完整版| 火花 电影| 爱你电视剧演员表| 儿童视力| 樊霖锋| 夏日福星 电影| douyin.com| 意外的春天| 夏日福星 电影| 魔女| 《流感》高清在线观看| 《欲望中的女人》| 庞勇| 往肚子里打气撑大肚子极限视频| 陈诗雅韩国| 大场久美子|

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

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

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

站長微信:lxwl520520

站長QQ:1737366103