【正文】
用 PEAR 重寫 Books 例子 使用 PEAR DB 包的時候,執(zhí)行的步驟和使用 PHP 函數(shù)是類似。增加新的模塊非常容易,只需要執(zhí)行“ pear packagename”就可以了。 托管 ISP 大部分人的 ISP 都安裝了 PEAR DB。 在執(zhí)行完這個批處理文件后,你可能需要編輯 文件,將 PEAR 的目錄加入到include_path 中。要修改系統(tǒng)的 PATH變量,選擇“開始 — 控制面板 — 系統(tǒng) — 環(huán)境變量”,在 PATH 變量的后面增加一項“ C:\php”。 圖 95: 安裝腳本 安裝程序會要求輸入幾個路徑,你可以使用默認(rèn)值。如果你在第二章沒有安裝所以文件,那么現(xiàn)在把所有的 PHP文件都解壓到 C:\php 下,然后執(zhí)行這個批處理文件。我們要用到的 DB包是可選的,但是它會被包管理器默認(rèn)安裝。 安裝 外文文獻原文及譯文 第 16 頁 共 6 頁 PEAR 使用包管理器來管理安裝 PEAR 模塊。使用 mysql_close來關(guān)閉一個數(shù)據(jù)庫,它會告訴 PHP 和 MySQL 這個數(shù)據(jù)庫連接已經(jīng)不再使用,所使用的所有資源和內(nèi)存都可以釋放。pages39。br / 39。Author: 39。title39。 Mysql_fetch_assoc 是使用 MYSQL_ASSOC 取結(jié)果的另外一種方式。它的參數(shù)是查 詢結(jié)果和一個可選的結(jié)果綁定方式。 } 結(jié)果行的所有列都保存在一個數(shù)組里,可以方便地進行訪問。 echo 39。 echo 39。 // Fetch and display the results while ($result_row = mysql_fetch_row(($result))){ echo 39。 取結(jié)果并顯示 使用 mysql_fetch_row 從結(jié)果集中取出一行,它的用法如下: array mysql_fetch_row ( resource $result)。 if (!$result){ die (Could not query the database: br /. mysql_error( ))。它有兩個參數(shù):查詢和可選的數(shù)據(jù)庫連接,返回值是查詢結(jié)果。 這個版本的代碼比下面的代碼要靈活多了: // Assign the query $query = SELECT * FROM books NATURAL JOIN authors。 books 39。 * 39。 注意 :跟使用 mysql 命令行客戶端不同 ,這里的查詢不需要以分號結(jié)尾。 構(gòu)建 SQL SELECT 查詢 構(gòu)建 SQL查詢非常容易就是將一個字符串賦值給變量。 if (!$db_select) { die (Could not select the database: br /. mysql_error( ))。 選擇數(shù)據(jù)庫 建立連接之后,下一步就是使用 mysql_select_db 來選擇我們要用的數(shù)據(jù)庫。解決這個問題需要將 文件從 PHP壓縮包例的 ext/目錄復(fù)制到 C:/php,并修改C:\WINDOWS\ 文件,確保下面兩行沒有被注釋掉(注釋的方法在行首使用分號)。對于大部分?jǐn)?shù)據(jù)庫驅(qū)動網(wǎng)頁來說,不能訪問數(shù)據(jù)庫就意味著網(wǎng)頁毫無用處。如果連接成功,就會返回新建立的連接,如果不能建立連接就會返回 FALSE。)。如例 94 所示,通過 include 包含連接信息的文件,我們可以在調(diào)用 mysql_connect 函數(shù)的時候使用這些變量而不是將這些值寫死在代碼中。 如果你在第 8章中沒有創(chuàng)建這些表,可以將例 93中的代碼保存成文件 ,然后在命令行執(zhí)行命令,命令格式如下: mysql u username ppassword D database_name 如果使用例子中的值,那么這個命令就是: mysql u test pyourpass D test 數(shù)據(jù)庫的名字叫 test,它包含三個表,分別是 books、 authors 和 purchases。 DHTML Cookbook39。),(2,39。,39。 CREATE TABLE purchases ( id int(11) NOT NULL auto_increment, user varchar(10) default NULL, title varchar(150) default NULL, day date default NULL, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1。,476),(2,39。 例 93:重建測試數(shù)據(jù)庫的 SQL 語句 DROP TABLE IF EXISTS books。yourpass39。test39。 ? 在例 92 中,我們創(chuàng)建的文件使用跟 Web服務(wù)器放在同一臺機器上的數(shù)據(jù)庫,并指定的數(shù)據(jù)庫的名字,用戶名和密碼。 $db_username=39。 例 91:設(shè)置數(shù)據(jù)庫登錄的配置文件模板 ?php $db_host=39。 注意:不用擔(dān)心有人會直接看到這個文件從而得到你的數(shù)據(jù)庫的登錄信息。 圖 91:使用數(shù)據(jù)庫時函數(shù) 和資源之間的交互 包含數(shù)據(jù)庫登錄細(xì)節(jié) 我們先創(chuàng)建一個文件,用來保存登錄 MySQL 所用到的信息。 我們需要同樣的信息來連接數(shù)據(jù)庫: ? 數(shù)據(jù)庫服務(wù)器的 IP地址 ? 數(shù)據(jù)庫的名字 ? 用戶名 ? 密碼 在開始之前,首先使用 MySQL 的命令行客戶端確認(rèn)你登錄到數(shù)據(jù)庫。這非常簡單,我們會用一些例子說明。第一個是連接的標(biāo)識符,它記錄了一個活動連接用來連接到數(shù)據(jù)庫所必需的所有信息。我們會詳細(xì)討論這兩種連接方式。同樣,一些 MySQL 特定的函數(shù)被更一般的、可以用在很多數(shù)據(jù)庫上的函數(shù)所替代。它將交互過程中無關(guān)緊要的部分屏蔽起來,讓你關(guān)注于重要的部分。 PHP 有標(biāo)準(zhǔn)的函數(shù)用來操作數(shù)據(jù)庫。 }。s value into the string. PHP treats newlines as whitespace in the manner of a freeform language (except when inside string quotes), and statements are terminated by a semicolon. PHP has three types of ment syntax: /* */ serves as block ments, and // as well as are used for inline ments. The echo statement is one of several facilities PHP provides to output text (. to a web browser). In terms of keywords and language syntax, PHP is similar to most high level languages that follow the C style syntax. If conditions, for and while loops, and function returns are similar in syntax to languages such as C, C++, Java and Perl. PHP stores whole numbers in a platformdependent range. This range is typically that of 32bit signed integers. Unsigned integers are converted 畢業(yè)論文 values in certain situations。 PEAR error reporting The function DB::isError will check to see whether the result that’s been returned to you is an error. If it is an error, you can use DB::errorMessage to return a text description of the error that was generated. You need to pass DB::errorMessage, the return value from your function, as an argument. Here you rewrite the PEAR code to use error checking: ?php if ( DB::isError( $demoResult = $dbquery( $sql))) { echo DB::errorMessage($demoResult)。Pages: 39。Author: 39。Title: 39。 if (!$db_select) { die (Could not select the database: br /. mysql_error( ))。外文文獻原文及譯文 第 1 頁 共 6 頁 外文文獻原文及譯文 Getting PHP to Talk to MySQl 通過 PHP訪問 MySQL 系 別: 專 業(yè): 2020 年 5 月 14 日 Getting PHP to Talk to MySQl Now that you’re fortable using the MySQL client tools to manipulate data in the database, you can begin using PHP to display and modify data from the database. PHP has standard functions for working with the database. First, we’re going to discuss PHP’s builtin database functions. We’ll also show you how to use the The PHP Extension and Application Repository (PEAR) database functions that provide the ability to use the same functions to access any supported database. This type of flexibility es from a process called abstraction. In programming interfaces, abstraction simplifies a plex interaction. It works by removing any nonessential parts of the interaction, allowing you to concentrate on the important parts. PEAR’s DB classes are one such database interface abstraction. The information you need to log into a database is reduced to the bare minimum. This standard format allows you to interact with MySQL, as well as other databases using the same functions. Similarly, other MySQLspecific functions are replaced with generic ones that know how to talk to many databases. For example, the MySQLspecific connect function is: mysql_connect($db_host, $db_username, $db_password)。 } Again, it’s good practice to check for an error and display it every time you access the database. Now that you’ve got a good database connection, you’re ready to execute your SQL query. Building the SQL SELECT Query 外文文獻原文及譯文 第 4 頁 共 6 頁 Building a SQL query is as easy as setting a variable to the string that is your SQL query. Of course, you’ll need to use a valid SQL query, or MySQL returns with an err