cvs: peardoc /en/core db.xml
| From: | Thomas V.V.Cox | Date: | Tue, 25 Dec 2001 20:39:48 +0000 |
| Subject: | cvs: peardoc /en/core db.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1683@lists.php.net to get a copy of this message | ||
cox Tue Dec 25 15:39:48 2001 EDT
Modified files:
/peardoc/en/core db.xml
Log:
more revisions
Index: peardoc/en/core/db.xml
diff -u peardoc/en/core/db.xml:1.7 peardoc/en/core/db.xml:1.8
--- peardoc/en/core/db.xml:1.7 Tue Dec 25 10:45:41 2001
+++ peardoc/en/core/db.xml Tue Dec 25 15:39:47 2001
@@ -1,5 +1,5 @@
<?xml encoding="iso-8859-1"?>
-<!-- $Revision: 1.7 $ -->
+<!-- $Revision: 1.8 $ -->
<reference id="core.db">
<title>PEAR DB: a unified API for accessing SQL-databases</title>
@@ -20,7 +20,7 @@
<simpara>
To connect to a database through PEAR::DB, you have to create a
valid <acronym>DSN - data source name</acronym>. This DSN
- consists of the:
+ consists in the following parts:
</simpara>
<para>
<simplelist>
@@ -56,13 +56,13 @@
</para>
<para>
The format of the supplied DSN is in its fullest form:
- <programlisting role="php">
+ <programlisting role="url">
phptype(dbsyntax)://username:password@protocol+hostspec/database
</programlisting>
Most variations are allowed:
- <programlisting role="php">
+ <programlisting role="url">
phptype://username:password@protocol+hostspec:110//usr/db_file.db
phptype://username:password@hostspec/database_name
phptype://username:password@hostspec
@@ -75,7 +75,7 @@
The currently supported database backends are:
- <programlisting role="php">
+ <programlisting role="configure">
mysql -> MySQL
pgsql -> PostgreSQL
ibase -> InterBase
@@ -94,9 +94,10 @@
<para>
Please note, that some features may be not supported by all
database backends. Please refer to the PEAR DB extensions
- status document located at: <parameter>pear base dir
- </parameter>/DB/STATUS to get a detailed list about what
- features are supported by which backend.
+ status document located at:
+ <quote><pear base dir>/DB/STATUS</quote>
+ to get a detailed list about what features are supported by which
+ backend.
</para>
</warning>
</para>
@@ -208,26 +209,27 @@
The <classname>DB_Result</classname> object provides two
functions to fetch rows: <function>fetchRow</function> and
<function>fetchInto</function>. <function>fetchRow</function>
- returns a row or false, when the result set is empty.
- <function>fetchInto</function> requires a variable, which will
+ returns the row, null on no more data or a <classname>DB_Error</classname>,
+ when an error occurs.
+ <function>fetchInto</function> requires a variable, which be will
directly assigned by reference to the result row. It will return
- false, when result set is empty too.
+ null when result set is empty or a <classname>DB_Error</classname> too.
</simpara>
<para>
<programlisting role="php">
<![CDATA[
<?php
-// Once you have a valid DB Result object
...
+$db = DB::connect($dsn);
+$res = $db->query("select * from mytable");
+
// Get each row of data on each iteration until
// there is no more rows
while ($row = $result->fetchRow()) {
$id = $row[0];
}
-?>
-
-<?php
-...
+// Or:
+// an example using fetchInto()
while ($result->fetchInto($row)) {
$id = $row[0];
}
@@ -253,11 +255,19 @@
<![CDATA[
<?php
$res = $db->query('select id, name, email from users');
-$row = $res->fetchRow($mode);
-$row = array (
+$row = $res->fetchRow(DB_FETCHMODE_ORDERED);
+/*
+$row will contain:
+array (
0 => <column "id" data>,
1 => <column "name" data>,
- 2 => <column "email" data>);
+ 2 => <column "email" data>
+)
+*/
+// Access the data with:
+$id = $row[0];
+$name = $row[1];
+$email = $row[2];
?>
]]>
</programlisting>
@@ -268,17 +278,25 @@
<parameter>DB_FETCHMODE_ASSOC</parameter>
</para>
<para>
- Returns an associative array with column
- names as array keys
+ Returns an associative array with the column
+ names as the array keys
<programlisting role="php">
<![CDATA[
<?php
$res = $db->query('select id, name, email from users');
-$row = $res->fetchRow($mode);
-$row = array (
- 0 => <column "id" data>,
- 1 => <column "name" data>,
- 2 => <column "email" data>);
+$row = $res->fetchRow(DB_FETCHMODE_ASSOC);
+/*
+$row will contain:
+array (
+ 'id' => <column "id" data>,
+ 'name' => <column "name" data>,
+ 'email' => <column "email" data>
+)
+*/
+// Access the data with:
+$id = $row['id'];
+$name = $row['name'];
+$email = $row['email'];
?>
]]>
</programlisting>
@@ -294,13 +312,21 @@
<programlisting role="php">
<![CDATA[
<?php
-$row = db_row Object
+$res = $db->query('select id, name, email from users');
+$row = $res->fetchRow(DB_FETCHMODE_OBJECT);
+/*
+$row will contain:
+db_row Object
(
[id] => <column "id" data>,
[name] => <column "name" data>,
[email] => <column "email" data>
)
-$id = $row->id;
+*/
+// Access the data with:
+$id = $row->id;
+$name = $row->name;
+$email = $row->email;
?>
]]>
</programlisting>
@@ -318,19 +344,15 @@
<programlisting role="php">
<![CDATA[
<?php
-...
// 1) Set the mode per call:
+
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
- [...]
-}
-while ($result->fetchInto($row, DB_FETCHMODE_ASSOC)) {
- [...]
+ $id = $row['id'];
}
// 2) Set the mode for all calls:
$db = DB::connect($dsn);
-
// this will set a default fetchmode for this Pear DB instance
// (for all queries)
$db->setFetchMode(DB_FETCHMODE_ASSOC);
@@ -418,7 +440,7 @@
</programlisting>
</para>
<para>
- <function>getRow</function> will fetch the first row and return
+ <function>getRow</function> returns the first row and return
it as an array
<programlisting role="php">
<![CDATA[
@@ -430,7 +452,7 @@
</programlisting>
</para>
<para>
- <function>getCol</function> will return an array with the data
+ <function>getCol</function> returns an array with the data
of the selected column. It accepts the column number to retrieve
as the second param.
<programlisting role="php">
@@ -442,10 +464,43 @@
$all_client_names = array('Stig', 'Jon', 'Colin');
</para>
<para>
+ <function>getAssoc</function> Fetch the entire result set of a query and return
it as an
+ associative array using the first column as the key.
+ <programlisting role="php">
+ <![CDATA[
+$data = getAssoc('SELECT name, surname, phone FROM mytable')
+/*
+Will return:
+array(
+ 'Peter' => array('Smith', '944679408'),
+ 'Tomas' => array('Cox', '944679408'),
+ 'Richard' => array('Merz', '944679408')
+)
+*/
+ ]]>
+ </programlisting>
+ </para>
+ <para>
+ <function>getAll</function> Fetch all the rows returned from a query
+ <programlisting role="php">
+ <![CDATA[
+$data = getAll('SELECT id, text, date FROM mytable');
+/*
+Will return:
+array(
+ 1 => array('4', 'four', '2004'),
+ 2 => array('5', 'five', '2005'),
+ 3 => array('6', 'six', '2006')
+)
+*/
+ ]]>
+ </programlisting>
+ </para>
+ <para>
The <function>get*</function> family methods will do all the
dirty job for you, this is: launch the query, fetch the data
and free the result. Please note that as all PEAR DB functions
- they will return a PEAR <classname>DB_error</classname> object
+ they will return a <classname>PEAR DB_error</classname> object
on errors.
</para>
</refsect2>