mysqli_fetch_array和MYSQLI_BOTH有什么区别?

时间:2021-09-07 20:11:52

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或它们实际上是不同的?

8 个解决方案

#1


24  

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 parameters which use to indicate what type of array will return.

这些是可选参数,用于指示将返回的数组类型。

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"
);

PHP Manual

#2


6  

mysqli_fetch_array()

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);
?>

MYSQLI_BOTH

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);
?>

#3


4  

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']访问结果。

#4


4  

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.

http://php.net/manual/en/mysqli-result.fetch-array.php

#5


3  

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.

通过使用MYSQLI_ASSOC常量,此函数的行为与mysqli_fetch_assoc()相同,而MYSQLI_NUM的行为与mysqli_fetch_row()函数相同。最后一个选项MYSQLI_BOTH将创建一个具有两者属性的单个数组。

Source

http://php.net/manual/en/mysqli-result.fetch-array.php

#6


3  

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);

#7


3  

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

2.Result Type(Optional)

1.Result

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

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]);

php manual

It means there is a no comparison between mysqli_fetch_array and MYSQLI_BOTH because is a default argument of mysqli_fetch_array.you can compare mysqli_fetch_array with mysqli_fetch_assoc

这意味着mysqli_fetch_array和MYSQLI_BOTH之间没有比较,因为它是mysqli_fetch_array的默认参数。你可以将mysqli_fetch_array与mysqli_fetch_assoc进行比较

#8


-2  

This is some example code that is pretty simple to follow if it helps. I use $stmt (it's just the way I learned how to do it) and I've used this countless times. I understand it may not be exactly what you needed, but this works and may help or help anyone else having a similar issue.

这是一些示例代码,如果它有帮助,可以很容易理解。我使用$ stmt(这就是我学习如何做的方式)而且我已经无数次地使用了它。我知道它可能不完全是您所需要的,但这可以帮助或帮助其他有类似问题的人。

include "../dbConnFile.php";

if($conn){

    if($_POST['example1']!='' && $_POST['example2']!=''){//checks input

        $stmt = $conn->prepare("INSERT INTO database (example1, example2) VALUES (?,?)");

        $stmt->bind_param("ss", $_POST['example1'], $_POST['example2']);

        $stmt->execute();
        $stmt->close();
    }

    $result=$conn->query("SELECT example1, example2 FROM datasbase ORDER BY ____ DESC");

    if($result){
        while($tempRowHolder = mysqli_fetch_array($result,MYSQL_ASSOC)){
            $records[] = $tempRowHolder;
        }
    }
}

#1


24  

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 parameters which use to indicate what type of array will return.

这些是可选参数,用于指示将返回的数组类型。

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"
);

PHP Manual

#2


6  

mysqli_fetch_array()

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);
?>

MYSQLI_BOTH

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);
?>

#3


4  

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']访问结果。

#4


4  

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.

http://php.net/manual/en/mysqli-result.fetch-array.php

#5


3  

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.

通过使用MYSQLI_ASSOC常量,此函数的行为与mysqli_fetch_assoc()相同,而MYSQLI_NUM的行为与mysqli_fetch_row()函数相同。最后一个选项MYSQLI_BOTH将创建一个具有两者属性的单个数组。

Source

http://php.net/manual/en/mysqli-result.fetch-array.php

#6


3  

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);

#7


3  

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

2.Result Type(Optional)

1.Result

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

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]);

php manual

It means there is a no comparison between mysqli_fetch_array and MYSQLI_BOTH because is a default argument of mysqli_fetch_array.you can compare mysqli_fetch_array with mysqli_fetch_assoc

这意味着mysqli_fetch_array和MYSQLI_BOTH之间没有比较,因为它是mysqli_fetch_array的默认参数。你可以将mysqli_fetch_array与mysqli_fetch_assoc进行比较

#8


-2  

This is some example code that is pretty simple to follow if it helps. I use $stmt (it's just the way I learned how to do it) and I've used this countless times. I understand it may not be exactly what you needed, but this works and may help or help anyone else having a similar issue.

这是一些示例代码,如果它有帮助,可以很容易理解。我使用$ stmt(这就是我学习如何做的方式)而且我已经无数次地使用了它。我知道它可能不完全是您所需要的,但这可以帮助或帮助其他有类似问题的人。

include "../dbConnFile.php";

if($conn){

    if($_POST['example1']!='' && $_POST['example2']!=''){//checks input

        $stmt = $conn->prepare("INSERT INTO database (example1, example2) VALUES (?,?)");

        $stmt->bind_param("ss", $_POST['example1'], $_POST['example2']);

        $stmt->execute();
        $stmt->close();
    }

    $result=$conn->query("SELECT example1, example2 FROM datasbase ORDER BY ____ DESC");

    if($result){
        while($tempRowHolder = mysqli_fetch_array($result,MYSQL_ASSOC)){
            $records[] = $tempRowHolder;
        }
    }
}