Reputation:
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?
Upvotes: 16
Views: 28906
Reputation: 1829
mysqli_fetch_array
— Fetch a result row as an associative, a numeric array, or both
there are two parameter in 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().
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)
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
you can use alternative mysqli_fetch_assoc($result) return result as a associative array
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
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
Upvotes: 5
Reputation: 1427
MYSQLI_BOTH
is equal to 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);
Upvotes: 6
Reputation: 16117
From the PHP Manual:
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.
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"
);
Upvotes: 33
Reputation: 23958
mysqli_fetch_array()
has second argument $resulttype
.
There are three options:
MYSQLI_ASSOC
, MYSQLI_NUM
, or MYSQLI_BOTH
.
Meanings:
MYSQLI_ASSOC
: Fetch associative array
MYSQLI_NUM
: Fetch numeric array
MYSQLI_BOTH
: Fetch both associative and numeric array.
MYSQLI_BOTH is default.
if we do not provide the second parameter, MYSQLI_BOTH
will be considered.
So, the statements:
$result = mysqli_fetch_array($res, MYSQLI_BOTH);
AND
$result = mysqli_fetch_array($res);
Are equal.
http://php.net/manual/en/mysqli-result.fetch-array.php
Upvotes: 6
Reputation: 2869
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]
.
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_assoc()
essentially allows you to access the result using the first method $result['name']
.
Upvotes: 5
Reputation: 22532
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);
?>
Upvotes: 7
Reputation: 4760
MYSQLI_BOTH will create a single array with the attributes of MYSQLI_NUM and 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
Upvotes: 5