Home >Backend Development >PHP Tutorial >Pear DB Beginner's Guide Tutorial Page 1/3_PHP Tutorial
1. Introduction This is a guide on how to use Pear DB extension. Pear DB provides a series of classes:
The following is an example of strp by step:
<font face="黑体">存放</font>Pear<font face="黑体">的目录:</font> <b># cd /usr/local/lib</b> <font face="黑体">用“</font>phpfi<font face="黑体">“口令登录</font>: <b># cvs -d :pserver:cvsread@cvs.php.net:/repository login</b> <font face="黑体">用以下命令得到所有的</font>pear<font face="黑体">文件,同时也可以用来更新已经下载的文件。其他的参数有:</font>"today", "last month",<font face="黑体">等。我推荐用</font>"last week"<font face="黑体">参数,因为一般</font>bugs<font face="黑体">的提交和修改都是每周一次。</font> <b># cvs -d :pserver:cvsread@cvs.php.net:/repository export -D "last week" php4/pear</b> <font face="黑体">编辑</font>php.ini<font face="黑体">文件加上下面一段在</font>include_path<font face="黑体">处:</font><i> /usr/local/lib/php4/pear</i> <font face="黑体">如果没有修改的权限,可以通过这条语句在代码中实现:</font> <i>ini_set('include_path', 'path_to_pear');</i> |
Get full documentation for PHP CVS
Note that PHP DB requires PHP version 4.0.4 or above, and some other packages in Pear such as XML Parser of the pear installer script require PHP 4.0.5 or above.
3. UsePear DB
3.1 Connect and disconnect from database
<code> </code> <code><?php</CODE><BR> <CODE>// The pear base directory must be in your include_path</CODE><BR> <CODE>require_once </CODE><CODE>'DB.php'</CODE><CODE>;</CODE><BR> <CODE>$user </CODE><CODE>= </CODE><CODE>'foo'</CODE><CODE>;</CODE><BR> <CODE>$pass </CODE><CODE>= </CODE><CODE>'bar'</CODE><CODE>;</CODE><BR> <CODE>$host </CODE><CODE>= </CODE><CODE>'localhost'</CODE><CODE>;</CODE><BR> <CODE>$db_name </CODE><CODE>= </CODE><CODE>'clients_db'</CODE><CODE>;</CODE><br><br> <CODE>// Data Source Name: This is the universal connection string</CODE><BR> <CODE>$dsn </CODE><CODE>= </CODE><CODE>"mysql://$user:$pass@$host/$db_name"</CODE><CODE>;</CODE><br><br> <CODE>// DB::connect will return a Pear DB object on success</CODE><BR> <CODE>// or a Pear DB Error object on error</CODE><BR> <CODE>// You can also set to TRUE the second param</CODE><BR> <CODE>// if you want a persistent connection:</CODE><BR> <CODE>// $db = DB::connect($dsn, true);</CODE><BR> <CODE>$db </CODE><CODE>= </CODE><CODE>DB</CODE><CODE>::</CODE><CODE>connect</CODE><CODE>(</CODE><CODE>$dsn</CODE><CODE>);</CODE><br><br> <CODE>// With DB::isError you can differentiate between an error or</CODE><BR> <CODE>// a valid connection.</CODE><BR> <CODE>if (</CODE><CODE>DB</CODE><CODE>::</CODE><CODE>isError</CODE><CODE>(</CODE><CODE>$db</CODE><CODE>)) {</CODE><BR> <CODE> die (</CODE><CODE>$db</CODE><CODE>-></code><code>getMessage</code><code>());</code><br> <code>}</code><br> <code>....</code><br> <code>// You can disconnect from the database with:</code><br> <code>$db</code><code>-></code><code>disconnect</code><code>();</code><br> <code>?></code><code></code> <code> </code> |
Data source($dsn Parameter) in the above example has the following allowed formats: (from parseDSN method of 🎜>Pear/DB.php)
<code> </code> <code> * phptype: Database backend used in PHP (mysql, odbc etc.)</code><br> <code> * dbsyntax: Database used with regards to SQL syntax etc.</code><br> <code> * protocol: Communication protocol to use (tcp, unix etc.)</code><br> <code> * hostspec: Host specification (hostname[:port])</code><br> <code> * database: Database to use on the DBMS server</code><br> <code> * username: User name for login</code><br> <code> * password: Password for login</code><br> <code> *</code><br> <code> * The format of the supplied DSN is in its fullest form:</code><br> <code> *</code><br> <code> * phptype(dbsyntax)://username:password@protocol+hostspec/database</code><br> <code> *</code><br> <code> * Most variations are allowed:</code><br> <code> *</code><br> <code> * phptype://username:password@protocol+hostspec:110//usr/db_file.db</code><br> <code> * phptype://username:password@hostspec/database_name</code><br> <code> * phptype://username:password@hostspec</code><br> <code> * phptype://username@hostspec</code><br> <code> * phptype://hostspec/database</code><br> <code> * phptype://hostspec</code><br> <code> * phptype(dbsyntax)</code><br> <code> * phptype</code><code></code> |
The databases currently supported are ( in the phptype DSN section):
<code> </code> <code>mysql -> MySQL</code><br> <code>pgsql -> PostgreSQL</code><br> <code>ibase -> InterBase</code><br> <code>msql -> Mini SQL</code><br> <code>mssql -> Microsoft SQL Server</code><br> <code>oci8 -> Oracle 7/8/8i</code><br> <code>odbc -> ODBC (Open Database Connectivity)</code><br> <code>sybase -> SyBase</code><br> <code>ifx -> Informix</code><br> <code>fbsql -> FrontBase</code><code></code> |
Note that not all database features are supported, you can start from the root directory > ;/DB/STATUS
<code> </code> <code><?php</CODE><BR> <CODE>// Once you have a valid DB object</CODE><BR> <CODE>...</CODE><BR> <CODE>$sql </CODE><CODE>= </CODE><CODE>"select * from clients"</CODE><CODE>;</CODE><BR> <CODE>// If the query is a "SELECT", $db->query will return</code><br> <code>// a DB Result object on success.</code><br> <code>// Else it simply will return a DB_OK</code><br> <code>// On failure it will return a DB Error object.</code><br> <code>$result </code><code>= </code><code>$db</code><code>-></code><code>query</code><code>(</code><code>$sql</code><code>);</code><br> <code>// Always check that $result is not an error</code><br> <code>if (</code><code>DB</code><code>::</code><code>isError</code><code>(</code><code>$result</code><code>)) {</code><br> <code> die (</code><code>$result</code><code>-></code><code>getMessage</code><code>());</code><br> <code>}</code><br> <code>....</code><br> <code>?></code><code></code> <code> </code> |
true
http: //www.bkjia.com/PHPjc/319757.html