php mysqli_fetch_array 和 MYSQLI_BOTH 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34648767/
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
What is difference between mysqli_fetch_array and MYSQLI_BOTH?
提问by devpro
I know that
我知道
mysqli_fetch_assoc,
mysqli_fetch_array,
mysqli_fetch
However, is MYSQLI_BOTH
equal to mysqli_fetch_array
or are they in fact different?
然而,是MYSQLI_BOTH
等于mysqli_fetch_array
还是实际上不同?
回答by devpro
From the PHP Manual:
来自 PHP 手册:
By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.
通过使用 MYSQLI_ASSOC 常量,该函数的行为将与 mysqli_fetch_assoc() 相同,而 MYSQLI_NUM 的行为将与 mysqli_fetch_row() 函数相同。最后一个选项 MYSQLI_BOTH 将创建一个具有两者属性的数组。
These are the optional parameterswhich use to indicate what type of array
will return.
这些是用于指示将返回什么类型的可选参数array
。
Here is the basic examples:
以下是基本示例:
$result->fetch_array(MYSQLI_NUM);
array(
0 => "val 1",
1 => "val 2"
);
$result->fetch_array(MYSQLI_ASSOC);
array(
'key0' => "val 1",
'key1' => "val 2"
);
$result->fetch_array(MYSQLI_BOTH);
array(
0 => "val 1",
'key0' => "val 1",
1 => "val 2",
'key1' => "val 2"
);
回答by Saty
function can also store the data in associative indices, using the field names of the result set as keys.
函数还可以将数据存储在关联索引中,使用结果集的字段名称作为键。
For example
例如
<?php
mysqli_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysqli_error());
mysqli_select_db("mydb");
$result = mysqli_query("SELECT id, name FROM mytable");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
printf ("ID: %s Name: %s", $row["id"], $row["name"]);
}
mysqli_free_result($result);
?>
Will create a single array with the attributes of both.
将创建一个具有两者属性的数组。
For example
例如
<?php
mysqli_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysqli_error());
mysqli_select_db("mydb");
$result = mysqli_query("SELECT id, name FROM mytable");
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
printf ("ID: %s Name: %s", $row[0], $row["name"]);
}
mysqli_free_result($result);
?>
回答by Pupil
mysqli_fetch_array()
has second argument $resulttype
.
有第二个论点$resulttype
。
There are three options:
共有三个选项:
MYSQLI_ASSOC
, MYSQLI_NUM
, or MYSQLI_BOTH
.
MYSQLI_ASSOC
、MYSQLI_NUM
、 或MYSQLI_BOTH
。
Meanings:
含义:
MYSQLI_ASSOC
: Fetch associative array
MYSQLI_ASSOC
: 获取关联数组
MYSQLI_NUM
: Fetch numeric array
MYSQLI_NUM
: 获取数值数组
MYSQLI_BOTH
: Fetch both associative and numeric array.
MYSQLI_BOTH
: 获取关联数组和数值数组。
MYSQLI_BOTH is default.
MYSQLI_BOTH 是默认值。
if we do not provide the second parameter, MYSQLI_BOTH
will be considered.
如果我们不提供第二个参数,MYSQLI_BOTH
将被考虑。
So, the statements:
所以,声明:
$result = mysqli_fetch_array($res, MYSQLI_BOTH);
$result = mysqli_fetch_array($res, MYSQLI_BOTH);
AND
和
$result = mysqli_fetch_array($res);
$result = mysqli_fetch_array($res);
Are equal.
是平等的。
回答by Ramalingam Perumal
MYSQLI_BOTH
is equal to mysqli_fetch_array
.
MYSQLI_BOTH
等于mysqli_fetch_array
。
You want both numeric and associative array then you can use
您需要数字和关联数组,然后您可以使用
mysqli_fetch_array($res);
OR
或者
mysqli_fetch_array($res, MYSQLI_BOTH);
回答by Clay
MYSQLI_BOTH will create a single array with the attributes of MYSQLI_NUM and MYSQLI_ASSOC.
MYSQLI_BOTH 将创建一个具有 MYSQLI_NUM 和 MYSQLI_ASSOC 属性的数组。
From the documentation:
从文档:
By using the MYSQLI_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while MYSQLI_NUM will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both.
Source
通过使用 MYSQLI_ASSOC 常量,该函数的行为将与 mysqli_fetch_assoc() 相同,而 MYSQLI_NUM 的行为将与 mysqli_fetch_row() 函数相同。最后一个选项 MYSQLI_BOTH 将创建一个具有两者属性的数组。
来源
回答by Matt
MYSQLI_BOTH
is an option in mysqli_fetch_array
that allows you to access arrays in an associative way e.g. $result['name']
and by index e.g. the number of the position in the result $result[0]
.
MYSQLI_BOTH
是一个选项,mysqli_fetch_array
它允许您以关联方式访问数组,例如$result['name']
,通过索引(例如结果中的位置编号)$result[0]
。
Within mysqli_fetch_array
, the second parameter allows the option for MYSQLI_NUM
which is using the index method only, MYSQLI_ASSOC
which you can access using the associate way and lastly MYSQLI_BOTH
which allows you to access the values either of the way.
在 中mysqli_fetch_array
,第二个参数只允许MYSQLI_NUM
使用索引方法的选项,MYSQLI_ASSOC
您可以使用关联方式访问该选项,最后MYSQLI_BOTH
允许您访问任何一种方式的值。
mysqli_fetch_assoc()
essentially allows you to access the result using the first method $result['name']
.
mysqli_fetch_assoc()
基本上允许您使用第一种方法访问结果$result['name']
。
回答by Parth Chavda
mysqli_fetch_array
— Fetch a result row as an associative, a numeric array, or both
mysqli_fetch_array
— 以关联数组、数值数组或两者的形式获取结果行
there are two parameter in mysqli_fetch_array
有两个参数 mysqli_fetch_array
1.Result
1.结果
2.Result Type(Optional)
2.结果类型(可选)
1.Result
1.结果
A result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result().
由mysqli_query()、mysqli_store_result() 或 mysqli_use_result()返回的结果集标识符。
example
例子
$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = mysqli_query($link, $query);
2.Result Type
2.结果类型
MYSQLI_BOTH
- Fetch a result row as an associative and a numeric array(Default Parameter)
MYSQLI_BOTH
- 获取结果行作为关联数组和数字数组(默认参数)
example
例子
/* associative and numeric array */
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);
MYSQLI_ASSOC
- Fetch a result row as an associative array
MYSQLI_ASSOC
- 获取结果行作为关联数组
you can use alternative mysqli_fetch_assoc($result) return result as a associative array
您可以使用替代 mysqli_fetch_assoc($result) 返回结果作为关联数组
example
例子
/* associative array */
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
MYSQLI_NUM
- Fetch a result row as a numeric array
MYSQLI_NUM
- 获取结果行作为数值数组
example
例子
/* numeric array */
$row = mysqli_fetch_array($result, MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);
It means there is a no comparison between
mysqli_fetch_array
andMYSQLI_BOTH
because is a default argument ofmysqli_fetch_array
.you can comparemysqli_fetch_array
withmysqli_fetch_assoc
这意味着在
mysqli_fetch_array
and之间没有可比性,MYSQLI_BOTH
因为是mysqli_fetch_array
.you的默认参数,可以mysqli_fetch_array
与mysqli_fetch_assoc