gpt4 book ai didi

php - mysql_fetch_array 和 mysql_fetch_row 之间的区别?

转载 作者:IT老高 更新时间:2023-10-28 23:58:25 25 4
gpt4 key购买 nike

这是 PHP 用户的一个简单问题。我无法得到 PHP 中 mysql_fetch_array()mysql_fetch_row() 之间的确切区别的原因是我一直在使用 Java。


在我在这里发布这个问题之前,我从谷歌得到了一些答案,但我发现它们有些令人困惑。我在网上找到的一些链接如下。

Answer 1

Answer 2

Answer 3

Answer 4


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

最佳答案

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

让我们创建一个表“tb”,以明确示例,其中包含三个字段“id”、“username”和“password”

表格:待定

在表中插入一个新行,id 为 1,用户名为 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()

以关联数组、数字数组的形式获取结果行,并且它同时通过关联数组和数字数组获取。

<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

关于php - mysql_fetch_array 和 mysql_fetch_row 之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8347565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com