On the official PHP page there are several examples but all with different names.
Searching the web it is spoken but in PDO, searching in StackOverflow the same is spoken in PDO.
But what does it mean in MySQLi object-oriented prepared statement procedures.
I have the following line of code.
$stmt->fetch();
What does it mean? What does that line of code do?
In mysqli,
fetch
it is a method or function that is applied to the objectmysqli_stmt
and that is used to read the results returned by said object.fetch
it could be translated in Spanish as going to look for , or bringing , recovering . This is what it doesfetch
in essence, bring us the results obtained.It
fetch
is generally used inside a loop (while, foreach
), because in a certain sense what this method does is place a pointer in the results obtained, starting with the first record, and then go through them row by row.That's why it's almost always
fetch
used like this:The methods derived from
fetch
The link you put in your question (which points to the different variants of
fetch
in PDO), opens the door for us to talk about other derived methodsfetch
that exist inmysqli
.As well as PDO (although this one allows many more and more interesting possibilities),
mysqli
you have several methods that we could call variants offetch
, that allow us to bring the results in a certain way according to the later use that we want to give to these results.It should be said that
fetch
it is only applicable to the objectmysqli_stmt
, as we said above. While these other methods can be applied to the object as wellmysqli_result
, to fetch/read the results returned by said objects 1 .It is convenient to enumerate these methods, we can find them in the Class Synopsis
mysqli_result
, described in the PHP Manual .They are the following:
fetch_all
: Get all the rows in an associative array, a numeric array, or both.fetch_array
: Get a result row as an associative array, numeric, or both.fetch_assoc
: Get a result row as an associative array.fetch_field_direct
: Gets the metadata of a single field.fetch_field
: Gets the next field of the result set.fetch_fields
: Gets an array of objects representing the fields of a result set.fetch_object
: Gets the current row of a result set as an object.fetch_row
: Get a result row as an enumerated array.It's not as rich as PDO , but, it's something :). In addition, it should be noted that in
mysqli
, for some of these methods to work, it is necessary to have the driver installedmysqlnd
, which is an added difficulty, which makes itmysqli
less attractive (at least for me), since you will be programming code that, in part, perhaps not work, depending on an external driver. For me it is a serious mistake of mysqli designers.I hope it helps you.
Grades:
1
fetch
on the contrary, is not applicable to the objectmysqli_result
. When we try to read the results returned by this object usingfetch
, we get the following exception:Call to undefined method mysqli_result::fetch()
. Note that this is applicable for the methodmysqli_query
, which is often used when there is no need for prepared queries, and whose result is also an objectmysqli_result
.What it does is fill the variables linked to the statement through the function
mysqli_stmt::bind_result()
, said function returns 2 valuesFALSE An error occurred
Taken from the php documentation http://php.net/manual/es/mysqli-stmt.fetch.php