2010-03-28 22:10:10 +00:00
<?xml version="1.0" encoding="utf-8"?>
2009-07-11 08:13:42 +00:00
<!-- $Revision$ -->
2008-01-02 13:58:46 +00:00
<refentry xml:id= "mysqli-result.fetch-assoc" xmlns= "http://docbook.org/ns/docbook" >
2007-01-27 17:37:36 +00:00
<refnamediv >
2008-01-02 13:58:46 +00:00
<refname > mysqli_result::fetch_assoc</refname>
2008-01-02 13:32:53 +00:00
<refname > mysqli_fetch_assoc</refname>
<refpurpose > Fetch a result row as an associative array</refpurpose>
2007-01-27 17:37:36 +00:00
</refnamediv>
2007-01-28 04:25:58 +00:00
<refsect1 role= "description" >
&reftitle.description;
2010-05-08 21:20:24 +00:00
<para > &style.oop; </para>
2012-01-10 08:41:49 +00:00
<methodsynopsis role= "oop" >
2021-03-13 10:09:58 +00:00
<modifier > public</modifier> <type class= "union" > <type > array</type> <type > null</type> <type > false</type> </type> <methodname > mysqli_result::fetch_assoc</methodname>
<void />
2008-01-02 13:58:46 +00:00
</methodsynopsis>
2010-05-08 21:20:24 +00:00
<para > &style.procedural; </para>
2021-03-13 10:09:58 +00:00
<methodsynopsis role= "procedural" >
<type class= "union" > <type > array</type> <type > null</type> <type > false</type> </type> <methodname > mysqli_fetch_assoc</methodname>
2007-01-27 17:37:36 +00:00
<methodparam > <type > mysqli_result</type> <parameter > result</parameter> </methodparam>
</methodsynopsis>
<para >
2008-01-02 13:32:53 +00:00
Returns an associative array that corresponds to the fetched row or &null;
if there are no more rows.
2007-01-27 17:37:36 +00:00
</para>
2008-01-02 13:32:53 +00:00
&database.field-case;
&database.fetch-null;
2007-01-28 04:25:58 +00:00
</refsect1>
<refsect1 role= "parameters" >
&reftitle.parameters;
2007-01-27 17:37:36 +00:00
<para >
2007-01-28 04:25:58 +00:00
<variablelist >
&mysqli.result.description;
</variablelist>
2007-01-27 17:37:36 +00:00
</para>
</refsect1>
2007-01-28 04:25:58 +00:00
<refsect1 role= "returnvalues" >
2007-01-27 17:37:36 +00:00
&reftitle.returnvalues;
<para >
2021-01-09 16:47:58 +00:00
Returns an associative array of values representing the fetched row in the result
2008-01-02 13:32:53 +00:00
set, where each key in the array represents the name of one of the result
2021-01-09 16:47:58 +00:00
set's columns or &null; if there are no more rows in result set.
2008-01-02 13:32:53 +00:00
</para>
<para >
If two or more columns of the result have the same field names, the last
column will take precedence. To access the other column(s) of the same
name, you either need to access the result with numeric indices by using
<function > mysqli_fetch_row</function> or add alias names.
2007-01-27 17:37:36 +00:00
</para>
</refsect1>
2007-01-28 04:25:58 +00:00
<refsect1 role= "examples" >
2007-01-27 17:37:36 +00:00
&reftitle.examples;
<example >
2021-01-09 16:47:58 +00:00
<title > <methodname > mysqli_result::fetch_assoc</methodname> example</title>
<para > &style.oop; </para>
2007-01-27 17:37:36 +00:00
<programlisting role= "php" >
2004-02-22 09:40:50 +00:00
< ![CDATA[
< ?php
2007-01-27 17:37:36 +00:00
2021-01-09 16:47:58 +00:00
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
$result = $mysqli->query($query);
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
2004-02-25 21:59:16 +00:00
}
2004-02-22 09:40:50 +00:00
]]>
2010-05-08 23:37:27 +00:00
</programlisting>
2021-01-09 16:47:58 +00:00
<para > &style.procedural; </para>
2007-01-27 17:37:36 +00:00
<programlisting role= "php" >
2004-02-22 09:40:50 +00:00
< ![CDATA[
< ?php
2004-02-25 21:59:16 +00:00
2021-01-09 16:47:58 +00:00
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect("localhost", "my_user", "my_password", "world");
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
$query = "SELECT Name, CountryCode FROM City ORDER BY ID DESC";
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
$result = mysqli_query($mysqli, $query);
2004-02-22 09:40:50 +00:00
2021-01-09 16:47:58 +00:00
/* fetch associative array */
while ($row = mysqli_fetch_assoc($result)) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
2004-02-25 21:59:16 +00:00
}
2004-02-22 09:40:50 +00:00
]]>
2007-01-27 17:37:36 +00:00
</programlisting>
2021-01-09 16:47:58 +00:00
&examples.outputs.similar;
2010-05-08 23:37:27 +00:00
<screen >
2004-02-25 21:59:16 +00:00
< ![CDATA[
Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)
2011-12-09 03:47:42 +00:00
]]>
</screen>
</example>
<example xml:id= "mysqli-result.example.iterator" >
2021-01-09 16:47:58 +00:00
<title > Comparison of <classname > mysqli_result</classname> <classname > iterator</classname> and <methodname > mysqli_result::fetch_assoc</methodname> usage</title>
<para >
<classname > mysqli_result</classname> can be iterated using &foreach; .
The result set will always be iterated from the first row, regardless of the current position.
</para>
2011-12-09 03:47:42 +00:00
<programlisting role= "php" >
< ![CDATA[
< ?php
2021-01-09 16:47:58 +00:00
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$query = 'SELECT Name, CountryCode FROM City ORDER BY ID DESC';
// Using iterators
$result = $mysqli->query($query);
foreach ($result as $row) {
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
2011-12-09 03:47:42 +00:00
}
echo "\n==================\n";
// Not using iterators
2021-01-09 16:47:58 +00:00
$result = $mysqli->query($query);
2011-12-09 03:47:42 +00:00
while ($row = $result->fetch_assoc()) {
2021-01-09 16:47:58 +00:00
printf("%s (%s)\n", $row["Name"], $row["CountryCode"]);
2011-12-09 03:47:42 +00:00
}
]]>
</programlisting>
&example.outputs.similar;
<screen >
< ![CDATA[
2021-01-09 16:47:58 +00:00
Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)
2011-12-09 03:47:42 +00:00
==================
2021-01-09 16:47:58 +00:00
Pueblo (USA)
Arvada (USA)
Cape Coral (USA)
Green Bay (USA)
Santa Clara (USA)
2011-12-09 03:47:42 +00:00
2004-02-25 21:59:16 +00:00
]]>
2010-05-08 23:37:27 +00:00
</screen>
</example>
2007-01-27 17:37:36 +00:00
</refsect1>
2007-01-28 04:25:58 +00:00
<refsect1 role= "seealso" >
&reftitle.seealso;
<para >
<simplelist >
<member > <function > mysqli_fetch_array</function> </member>
2021-10-19 08:31:48 +00:00
<member > <function > mysqli_fetch_column</function> </member>
2008-01-02 13:32:53 +00:00
<member > <function > mysqli_fetch_row</function> </member>
2007-01-28 04:25:58 +00:00
<member > <function > mysqli_fetch_object</function> </member>
<member > <function > mysqli_query</function> </member>
<member > <function > mysqli_data_seek</function> </member>
</simplelist>
</para>
</refsect1>
2007-01-27 17:37:36 +00:00
</refentry>
2003-03-15 23:01:35 +00:00
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
2009-09-25 07:04:39 +00:00
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
2003-03-15 23:01:35 +00:00
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->