mysql_fetch_array和mysql_fetch_row之间的区别?

时间:2011-12-01 20:05:09

标签: php mysql

对于PHP用户来说,这是一个简单的问题。我无法在PHP中获得mysql_fetch_array()mysql_fetch_row()之间的确切差异的原因是我一直在使用Java。


在我在这里发布这个问题之前,我从谷歌得到了一些答案,但我发现他们有点混乱。我在互联网上找到的一些链接如下。

Answer 1

Answer 2

Answer 3

Answer 4


我无法从上述答案中得到确切的想法。那么实际上它们之间究竟有什么区别呢?

10 个答案:

答案 0 :(得分:58)

许多php编程新手对mysql_fetch_array(),mysql_fetch_row(),mysql_fetch_assoc()和mysql_fetch_object()函数感到困惑,但所有这些函数都执行类似的过程。

让我们创建一个表“tb”,用于显示三个字段“id”,“username”和“password”

的清晰示例

表:tb

在表中插入一个新行,其值为1表示id,tobby表示用户名,tobby78 $ 2表示密码

enter image description here

<强> db.php中

<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>

<强> mysql_fetch_row()能够

获取结果行作为数值数组

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_row($query);
echo $row[0];
echo $row[1];
echo $row[2];
?>
</html>

结果

1 tobby tobby78 $ 2

<强> mysql_fetch_object()

获取结果行作为对象

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_object($query);
echo $row->id;
echo $row->username;
echo $row->password;
?>
</html>

结果

1 tobby tobby78 $ 2

<强> mysql_fetch_assoc()

获取结果行作为关联数组

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_assoc($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];
?>
</html> 

结果

1 tobby tobby78 $ 2

<强> mysql_fetch_array()

获取结果行作为关联数组,数字数组,并且它也通过associative&amp;提取。数组。

<html>
<?php
include('db.php');
$query=mysql_query("select * from tb");
$row=mysql_fetch_array($query);
echo $row['id'];
echo $row['username'];
echo $row['password'];

<span style="color: #993300;">/* here both associative array and numeric array will work. */</span>

echo $row[0];
echo $row[1];
echo $row[2];

?>
</html>

结果

1 tobby tobby78 $ 2

答案 1 :(得分:32)

documentation对此很清楚,你看过吗?

mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )
  

返回与获取的行对应的字符串数组,或   如果没有更多行,则为FALSE。 返回数组的类型取决于   如何定义result_type。通过使用MYSQL_BOTH(默认),您将获得   包含关联索引和数字索引的数组。使用MYSQL_ASSOC,   你只获得关联索引(如mysql_fetch_assoc()工作), [by] using   MYSQL_NUM,你只获得数字索引(因为mysql_fetch_row()有效)

mysql_fetch_row ( resource $result )
  

返回与获取的字符串对应的数字字符串数组   row,如果没有更多行,则返回FALSE。

     

mysql_fetch_row()从关联的结果中获取一行数据   使用指定的结果标识符。 该行以数组形式返回。   每个结果列都存储在一个数组偏移量中,从偏移量0开始。

总结

mysql_fetch_array( $result, MYSQL_ASSOC ) = mysql_fetch_assoc( $result ) mysql_fetch_array( $result, MYSQL_NUM ) = mysql_fetch_row( $result )

并且

mysql_fetch_array ( $result ) = mysql_fetch_assoc( $result ) + mysql_fetch_row( $result )

答案 2 :(得分:5)

mysql_fetch_object {row / array / assoc}()函数以相应的格式收集第一个匹配记录,并可以相应地进行检索。

con.php

<?php
        $host     = $_GET['host'];
        $username = $_GET['username'];
        $pass     = $_GET['pass'];
        $database  = $_GET['database'];
        $connect=new connect($host,$username,$pass,$database);
        class connect{
             function __construct($host,$user,$password,$db_name){
                mysql_connect($host,$user,$password) or die("Connection error");
                mysql_select_db($db_name);
                $error=mysql_error();
                if (!empty($error))
                {
                    echo $error;
                }
            }
        }
        ?>

enter image description here

<强>的index.php

<?php
$query=mysql_query("select * from category");
?>

在浏览器中转储数组时每个值的显示方式

<强> mysql_fetch_array

$row=mysql_fetch_array($query);
var_dump($row);

输出

array
  0 => string '1' (length=1)
  'id' => string '1' (length=1)
  1 => string '1' (length=1)
  'createdBy' => string '1' (length=1)
  2 => string 'APTITUDE' (length=8)
  'catName' => string 'APTITUDE' (length=8)
  3 => string 'APTITUDE' (length=8)
  'description' => string 'APTITUDE' (length=8)
  4 => string '1' (length=1)
  'status' => string '1' (length=1)

<强>和mysql_fetch_row

$row=mysql_fetch_row($query);
var_dump($row);

输出

array
  0 => string '1' (length=1)
  1 => string '1' (length=1)
  2 => string 'APTITUDE' (length=8)
  3 => string 'APTITUDE' (length=8)
  4 => string '1' (length=1)

<强> mysql_fetch_assoc

$row=mysql_fetch_assoc($query);
var_dump($row);

输出

array
  'id' => string '1' (length=1)
  'createdBy' => string '1' (length=1)
  'catName' => string 'APTITUDE' (length=8)
  'description' => string 'APTITUDE' (length=8)
  'status' => string '1' (length=1)

<强> mysql_fetch_object

$row=mysql_fetch_object($query);
var_dump($row);

输出

object(stdClass)[2]
  public 'id' => string '1' (length=1)
  public 'createdBy' => string '1' (length=1)
  public 'catName' => string 'APTITUDE' (length=8)
  public 'description' => string 'APTITUDE' (length=8)
  public 'status' => string '1' (length=1)

Rest @Gaurang提供了如何使用out代码和其他活动。

答案 3 :(得分:2)

mysql_fetch_array,因为manual says可以根据所选的result_type返回基于int(位置)的索引,关联数组或两者。

另一方面,

mysql_fetch_row总是返回基于整数索引的结果集。

我个人建议你使用mysql_fetch_array传递MYSQL_ASSOC作为第二个参数,因为总是更容易知道你想要获取哪个字段

答案 4 :(得分:2)

mysql_fetch_row返回一个枚举数组,因此索引是数字。 mysql_fetch_array返回一个关联数组(默认情况下,为索引合并数字),因此,mysql_fetch_array默认返回单个数组中复制的所有数据,一组数据使用数字作为索引,另一组数据仍然在同一个数组中,使用关联索引(基于文本的索引)。

mysql_fetch_row示例:

Array(2)
    0 => "foo"
    1 => "bar"

mysql_fetch_array示例(默认行为):

Array(4)
    0 => "foo"
    1 => "bar"
    "user" => "foo"
    "name" => "bar"

答案 5 :(得分:2)

有3个功能。     mysql_fetch_assoc     和mysql_fetch_row     mysql_fetch_array(row和assoc的组合)

我建议使用_assoc或_row来优化代码并保持清晰。如果你要抓一个列,请使用     $ row = mysql_fetch_row     $行[0]

答案 6 :(得分:1)

正如手册所说:mysql_fetch_array()可以返回关联数组,具体取决于默认值及其第二个参数(MYSQL_ASSOCMYSQL_NUM或{{1} })。

虽然mysql_fetch_row()始终返回索引数组。

答案 7 :(得分:1)

获取行返回当前条目的数值数组

http://www.php.net/manual/en/function.mysql-fetch-row.php

获取数组默认会返回一个完整的id =&gt; key =&gt;值数组,但它也提供了选择数字或关联返回的选项

http://www.php.net/manual/en/function.mysql-fetch-array.php

答案 8 :(得分:1)

您提供的第一个链接中的答案是正确的。评论说:

“而是两者都返回表中的所有行 它们之间的区别是fetch_array返回结果 关联数组以及数值数组,您也可以 通过提供指定所需的特定类型的数组 fetch_row返回时函数的第二个参数 只导致数字数组。“

...所以使用fetch_row你不能做像

这样的事情
echo $result['name']; // can do this in fetch_array, not in fetch_row
echo $result[0]; // can do this in fetch_array and fetch_row

答案 9 :(得分:0)

mysql_fetch_object以对象的形式从数据库返回结果,而mysql_fetch_array以数组的形式返回结果。这将允许通过字段名称访问数据。

相关问题