philip Sun Nov 10 02:02:40 2002 EDT
Modified files:
/phpdoc/en/reference/mysql/functions mysql-fetch-assoc.xml
Log:
Expanded the example as a place to point 'beginners' to. See also mysql_error().
Index: phpdoc/en/reference/mysql/functions/mysql-fetch-assoc.xml
diff -u phpdoc/en/reference/mysql/functions/mysql-fetch-assoc.xml:1.3
phpdoc/en/reference/mysql/functions/mysql-fetch-assoc.xml:1.4
--- phpdoc/en/reference/mysql/functions/mysql-fetch-assoc.xml:1.3 Sun Apr 21
11:23:30 2002
+++ phpdoc/en/reference/mysql/functions/mysql-fetch-assoc.xml Sun Nov 10 02:02:39
+2002
@@ -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);
+
+ $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["user_id"];
+ 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>
--
PHP Documentation Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php