Expanded the example as a place to point 'beginners' to. See also mysql_error().

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@103153 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Philip Olson 2002-11-10 07:02:39 +00:00
parent 42356b086e
commit df874ac38f

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.3 $ -->
<!-- $Revision: 1.4 $ -->
<!-- splitted from ./en/functions/mysql.xml, last change in rev 1.45 -->
<refentry id="function.mysql-fetch-assoc">
<refnamediv>
@ -42,28 +42,61 @@
provides a significant added value.
</para>
<example>
<title><function>mysql_fetch_assoc</function></title>
<title>An expanded <function>mysql_fetch_assoc</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("mydb");
$query = "select * from table";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo $row["user_id"];
echo $row["fullname"];
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydb: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
?>
]]>
</programlisting>
</example>
<para>
For further details, see also
See also
<function>mysql_fetch_row</function>,
<function>mysql_fetch_array</function> and
<function>mysql_query</function>.
<function>mysql_fetch_array</function>,
<function>mysql_query</function>, and
<function>mysql_error</function>.
</para>
</refsect1>
</refentry>