php mysql_fetch_array 和 mysql_fetch_row 的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8347565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between mysql_fetch_array and mysql_fetch_row?
提问by Lion
This is a simple question for PHP users. The reason I couldn't get the the exact difference between mysql_fetch_array()
and mysql_fetch_row()
in PHP is that I had been working much with Java.
对于 PHP 用户来说,这是一个简单的问题。我无法得到PHPmysql_fetch_array()
和mysql_fetch_row()
PHP之间的确切区别的原因是我一直在使用 Java。
Before I post this question here, I got some answers from Google but I found they're somewhat confusing. Some of the links I found on the internet are as follows.
在我在这里发布这个问题之前,我从谷歌那里得到了一些答案,但我发现它们有些令人困惑。我在互联网上找到的一些链接如下。
I couldn't get the exact idea from the above answers. So actually what is the exact difference between them?
我无法从上述答案中得到确切的想法。那么实际上它们之间的确切区别是什么?
回答by Gaurang
Many of the php programming newbies get confused about mysql_fetch_array(), mysql_fetch_row(), mysql_fetch_assoc() and mysql_fetch_object() functions, but all of these functions performs a similar process.
许多 php 编程新手对 mysql_fetch_array()、mysql_fetch_row()、mysql_fetch_assoc() 和 mysql_fetch_object() 函数感到困惑,但所有这些函数都执行类似的过程。
Let us create a table “tb” for clear example with three fields “id”, “username” and “password”
让我们创建一个表“tb”作为清晰的例子,其中包含三个字段“id”、“username”和“password”
Table: tb
表:tb
Insert a new row into the table with values 1 for id, tobby for username and tobby78$2 for password
在表中插入一个新行,id 为 1,用户名为 tobby,密码为 tobby78$2
db.php
数据库文件
<?php
$query=mysql_connect("localhost","root","");
mysql_select_db("tobby",$query);
?>
mysql_fetch_row()
mysql_fetch_row()
Fetch a result row as an numeric array
获取结果行作为数值数组
<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>
Result
结果
1 tobby tobby78$2
1 tobby tobby78$2
mysql_fetch_object()
mysql_fetch_object()
Fetch a result row as an 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>
Result
结果
1 tobby tobby78$2
1 tobby tobby78$2
mysql_fetch_assoc()
mysql_fetch_assoc()
Fetch a result row as an associative array
获取结果行作为关联数组
<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>
Result
结果
1 tobby tobby78$2
1 tobby tobby78$2
mysql_fetch_array()
mysql_fetch_array()
Fetch a result row as an associative array, a numeric array and also it fetches by both associative & numeric 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>
Result
结果
1 tobby tobby78$2
1 tobby tobby78$2
回答by aziz punjani
The documentationis pretty clear on this, have you looked at it ?
该文件是对这个很清楚,有你看了吗?
mysql_fetch_array ( resource $result [, int $result_type = 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), [by] using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).
返回与获取的行对应的字符串数组,如果没有更多行,则返回 FALSE。返回数组的类型取决于 result_type 的定义方式。通过使用 MYSQL_BOTH(默认),您将获得一个具有关联索引和数字索引的数组。使用 MYSQL_ASSOC,您只能获得关联索引(如 mysql_fetch_assoc() 工作),[通过] 使用 MYSQL_NUM,您只能获得数字索引(如 mysql_fetch_row() 工作)。
mysql_fetch_row ( resource $result )
Returns an numerical array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
mysql_fetch_row() fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.
返回与获取的行相对应的字符串数字数组,如果没有更多行,则返回 FALSE。
mysql_fetch_row() 从与指定结果标识符关联的结果中获取一行数据。该行作为数组返回。每个结果列都存储在一个数组偏移量中,从偏移量 0 开始。
In summary
总之
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_ASSOC )
= mysql_fetch_assoc( $result )
mysql_fetch_array( $result, MYSQL_NUM )
=mysql_fetch_row( $result )
And
和
mysql_fetch_array ( $result )
= mysql_fetch_assoc( $result )
+ mysql_fetch_row( $result )
mysql_fetch_array ( $result )
= mysql_fetch_assoc( $result )
+mysql_fetch_row( $result )
回答by Mr AJ
The mysql_fetch_object{row/array/assoc}() function collects the first single matching record in its respective format and can be retrieved accordingly.
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;
}
}
}
?>
index.php
索引.php
<?php
$query=mysql_query("select * from category");
?>
How each value will look on dumping the array in browser
每个值在浏览器中转储数组时的外观
mysql_fetch_array
mysql_fetch_array
$row=mysql_fetch_array($query);
var_dump($row);
Output:
输出:
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
mysql_fetch_row
$row=mysql_fetch_row($query);
var_dump($row);
Output:
输出:
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
mysql_fetch_assoc
$row=mysql_fetch_assoc($query);
var_dump($row);
Output:
输出:
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
mysql_fetch_object
$row=mysql_fetch_object($query);
var_dump($row);
Output:
输出:
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 has provided how to use the out for your code and other activities.
Rest @Gaurang 提供了如何将 out 用于您的代码和其他活动。
回答by Gabriel Sosa
mysql_fetch_array as the manual sayscan return an int (position) based index, associative array or both according to the result_type chosen.
mysql_fetch_array 如手册所述,可以根据所选的 result_type 返回基于 int(位置)的索引、关联数组或两者。
in the other hand mysql_fetch_rowalways return the result set based on integer index.
另一方面mysql_fetch_row总是返回基于整数索引的结果集。
I personally recommend you to use mysql_fetch_array passing MYSQL_ASSOC as second parameter since is always easier to know what field you would like to fetch
我个人建议您使用 mysql_fetch_array 传递 MYSQL_ASSOC 作为第二个参数,因为总是更容易知道您想要获取哪个字段
回答by jValdron
mysql_fetch_row returns an enumerated array, so the index are numbers. mysql_fetch_array returns an associative array (and by defaults, merge numbers for index), so, mysql_fetch_array returns, by default, all data duplicated in a single array, one set of data using number as index and another set, still in the same array, with associative index (text based indexes).
mysql_fetch_row 返回一个枚举数组,所以索引是数字。mysql_fetch_array 返回一个关联数组(默认情况下,合并索引的数字),因此,默认情况下,mysql_fetch_array 返回单个数组中复制的所有数据,一组数据使用数字作为索引,另一组数据仍然在同一个数组中,带有关联索引(基于文本的索引)。
mysql_fetch_row Example:
mysql_fetch_row 示例:
Array(2)
0 => "foo"
1 => "bar"
mysql_fetch_array Example (default behavior):
mysql_fetch_array 示例(默认行为):
Array(4)
0 => "foo"
1 => "bar"
"user" => "foo"
"name" => "bar"
回答by Peter Postma
There are 3 functions. mysql_fetch_assoc mysql_fetch_row mysql_fetch_array (a combination of row and assoc)
有3个功能。mysql_fetch_assoc mysql_fetch_row mysql_fetch_array (row 和 assoc 的组合)
I would recommend _assoc or _row to optimize your code and keep it clear. If you're grabbing a single column, use $row = mysql_fetch_row $row[0]
我会推荐 _assoc 或 _row 来优化你的代码并保持清晰。如果您要抓取单列,请使用 $row = mysql_fetch_row $row[0]
回答by mario
As the manual says: mysql_fetch_array()
can return an associativearray depending on defaults and its second parameter (MYSQL_ASSOC
, MYSQL_NUM
, or MYSQL_BOTH
).
由于手册说:mysql_fetch_array()
可以返回一个关联取决于默认值和其第二个参数阵列(MYSQL_ASSOC
,MYSQL_NUM
,或MYSQL_BOTH
)。
While mysql_fetch_row()
always returns an indexedarray.
虽然mysql_fetch_row()
总是返回一个索引数组。
回答by CBusBus
Fetch row returns a numerical array for current entry
获取行返回当前条目的数字数组
http://www.php.net/manual/en/function.mysql-fetch-row.php
http://www.php.net/manual/en/function.mysql-fetch-row.php
Fetch array will by default return a full id=>key=>value array but it also offers the option of choosing either numerical or associative return
默认情况下,获取数组将返回一个完整的 id=>key=>value 数组,但它也提供了选择数字或关联返回的选项
回答by Boundless
Answer two from the first link you provided is correct. The comment said:
您提供的第一个链接中的两个答案是正确的。评论说:
"Instead both returns all the rows from table. The difference between them is fetch_array return result in assoc array as well as numeric array and you can also specify which specific type of array you want by providing second parameter to the function while fetch_row return result in numeric array only."
“相反,两者都返回表中的所有行。它们之间的区别在于 fetch_array 返回 assoc 数组和数值数组中的结果,您还可以通过向函数提供第二个参数来指定所需的特定类型的数组,而 fetch_row 返回结果为仅限数字数组。”
... so using fetch_row you can't do something like
...所以使用 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
回答by iamasp
Mysql_fetch_object returns the result from the database as objects while mysql_fetch_array returns result as an array. This will allow access to the data by the field names.
mysql_fetch_object 以对象形式返回数据库中的结果,而 mysql_fetch_array 以数组形式返回结果。这将允许通过字段名称访问数据。