茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1671828 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
HoneyMoon_Day2_00046.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

DB研討會 : [轉貼] mysql_fetch_array() 與 mysql_fetch_assoc() 與 mysql_fetch_row() 的差異

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼] mysql_fetch_array() 與 mysql_fetch_assoc() 與 mysql_fetch_row() 的差異
[PHP] mysql_fetch_array() 與 mysql_fetch_assoc() 與 mysql_fetch_row() 的差異
當需要從DB讀取資料時,我們常會使用下列的語法:
while ($row = mysql_fetch_array($result)){
    $xxx = $row['a'];
}


但是,有時候也會看到 mysql_fetch_assoc() 或 mysql_fetch_row() ,差別在於...

mysql_fetch_array()
從資料集取得的陣列,索引值可以是數字(數字索引)或字串(關聯索引)。
$a=$row[0]; 或 $a=$row["a"];


mysql_fetch_assoc()
從資料集取得的陣列,索引值只能是字串(關聯索引)。
$a=$row["a"];


mysql_fetch_row()
從資料集取得的陣列,索引值只能是數字(數字索引)。
$a=$row[0];



所以,如果你想要讓陣列可以用數字當索引,同時也能用字串當索引,那就用 mysql_fatch_array() 吧。



原文出處:謝晒的PHP網頁設計: [PHP] mysql_fetch_array() 與 mysql_fetch_assoc() 與 mysql_fetch_row() 的差異
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[文件] mysql_fetch_assoc
mysql_fetch_assoc
(PHP 4 >= 4.0.3, PHP 5)

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

说明
array mysql_fetch_assoc ( resource $result )

返回对应结果集的关联数组,并且继续移动内部数据指针。 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 值。



原文出處:PHP: mysql_fetch_assoc - Manual (官方文件)
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼] mysql_fetch_assoc 範例
Example #1
<?php
$mysql = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = $mysql->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    $result->free();
}

/* close connection */
$mysql->close();
?>

Example #2
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";

if ($result = mysqli_query($link, $query)) {

    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

    /* free result set */
    mysqli_free_result($result);
}

/* close connection */
mysqli_close($link);
?>

以上例程会输出:
Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)

Example #3
<?php
$c = mysqli_connect('127.0.0.1','user', 'pass');

// Using iterators (support was added with PHP 5.4)
foreach ( $c->query('SELECT user,host FROM mysql.user') as $row ) {
    printf("'%s'@'%s'\n", $row['user'], $row['host']);
}

echo "\n==================\n";

// Not using iterators
$result = $c->query('SELECT user,host FROM mysql.user');
while ($row = $result->fetch_assoc()) {
    printf("'%s'@'%s'\n", $row['user'], $row['host']);
}

?>

以上例程的输出类似于:
'root'@'192.168.1.1'
'root'@'127.0.0.1'
'dude'@'localhost'
'lebowski'@'localhost'

==================

'root'@'192.168.1.1'
'root'@'127.0.0.1'
'dude'@'localhost'
'lebowski'@'localhost'


冷日:簡單來說,就是可以直接用 Table 的 Colnum Name 來做 index 取得 Row 裡面該行的內容!



原文出處:PHP: mysqli_result::fetch_assoc - Manual
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|