PHP畢業(yè)設(shè)計(jì)論文外文翻譯_第1頁(yè)
PHP畢業(yè)設(shè)計(jì)論文外文翻譯_第2頁(yè)
PHP畢業(yè)設(shè)計(jì)論文外文翻譯_第3頁(yè)
PHP畢業(yè)設(shè)計(jì)論文外文翻譯_第4頁(yè)
PHP畢業(yè)設(shè)計(jì)論文外文翻譯_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、英文原文名 Database Handling With PHP/MySQL中文譯名 數(shù)據(jù)庫(kù)處理PHP/MySQL 英文原文版出處:(美)Chris Shiflett, Marcus Baker PHP in Action M.2010-01-01:361-430譯文成績(jī): 指導(dǎo)教師(導(dǎo)師組長(zhǎng))簽名: 譯文:與數(shù)據(jù)庫(kù)通信不同其他腳本語(yǔ)言的Web頁(yè)面開發(fā),即ASP ,PHP是開源的,跨平臺(tái)的,并提供良好的連接,今天的大多數(shù)常見的數(shù)據(jù)庫(kù),包括Oracle , Sybase的Microsoft SQL Server ,MySQL和PostgreSQL , ODBC 。 PHP還提供了集成各種外部庫(kù),

2、使開發(fā)人員能夠生成PDF文檔,訪問安全的支付服務(wù),并產(chǎn)生圖形輸出以解析XML 。回頭看看第一原理在我們的飛躍的同時(shí)要簡(jiǎn)短的回顧一下,去提醒我們正在努力朝著自己的目標(biāo)。我們有兩個(gè)強(qiáng)大的,新的工具的在我們的面前:PHP腳本語(yǔ)言和MySQL數(shù)據(jù)庫(kù)引擎。重要的是要了解這兩個(gè)怎么會(huì)結(jié)合在一起。一個(gè)數(shù)據(jù)庫(kù)驅(qū)動(dòng)的Web站點(diǎn)的整個(gè)想法是讓網(wǎng)站的內(nèi)容駐留在數(shù)據(jù)庫(kù)中,并從數(shù)據(jù)庫(kù)中動(dòng)態(tài)地抽取數(shù)據(jù)來(lái)創(chuàng)建網(wǎng)頁(yè),來(lái)讓人用一個(gè)普通的web瀏覽器查看。因此,在系統(tǒng)的一端你有一個(gè)訪問者到你的網(wǎng)站使用Web瀏覽器來(lái)加載 ,期望看到一個(gè)standardHTML網(wǎng)頁(yè)。在另一端你有你的網(wǎng)站,其中一部分在一個(gè)MySQL數(shù)據(jù)庫(kù)中的一個(gè)或以

3、上的表,只有懂得何為SQL查詢(命令)才能作出回應(yīng)的內(nèi)容。   PHP腳本語(yǔ)言是穿針引線于兩種語(yǔ)言之間,它處理頁(yè)面請(qǐng)求,并獲取從MySQL數(shù)據(jù)庫(kù)中的數(shù)據(jù),然后動(dòng)態(tài)地得出瀏覽器期望得到的格式化的HTML頁(yè)面。使用PHP,你可以寫網(wǎng)站(花哨的圖形和頁(yè)面布局)像常規(guī)HTML的 “模板” 。其中內(nèi)容屬于這些模板,你可以使用一些PHP代碼來(lái)連接MySQL數(shù)據(jù)庫(kù)和 - 使用SQL查詢檢索并顯示在它地方的一些內(nèi)容。當(dāng)有人訪問我們的數(shù)據(jù)庫(kù)驅(qū)動(dòng)的Web站點(diǎn)的頁(yè)面會(huì)發(fā)生什么這是很清楚和明顯的:使用標(biāo)準(zhǔn)的URL訪問者的Web瀏覽器請(qǐng)求的網(wǎng)頁(yè) Web服務(wù)器軟件( Apache的,IIS或WH at

4、ever )認(rèn)識(shí)到所需的文件是一個(gè)PHP腳本,所以使用它的PHP插件,響應(yīng)頁(yè)面請(qǐng)求之前,服務(wù)器解釋該文件。某些PHP命令(我們還沒有學(xué)會(huì))連接到MySQL數(shù)據(jù)庫(kù),并要求所屬在Web頁(yè)面的內(nèi)容。 MySQL數(shù)據(jù)庫(kù)通過發(fā)送所請(qǐng)求的內(nèi)容到PHP腳本。 PHP腳本存儲(chǔ)內(nèi)容到一個(gè)或多個(gè)PHP變量,然后使用現(xiàn)在,熟悉的echo函數(shù)來(lái)輸出內(nèi)容作為網(wǎng)頁(yè)的一部分。 PHP的插件,通過分發(fā)它創(chuàng)造到Web服務(wù)器的HTML副本結(jié)束了。 Web服務(wù)器發(fā)送的HTML到Web瀏覽器,因?yàn)樗鼤?huì)一個(gè)純HTML文件,不同之處在于,而不是直接從HTML文件來(lái)了,該網(wǎng)頁(yè)是由PHP插件提供的輸出。用PHP 連接到MySQL在

5、你可以得到的內(nèi)容用來(lái)提供載入網(wǎng)頁(yè)的MySQL數(shù)據(jù)庫(kù)之前,你必須知道如何從一個(gè)PHP腳本中建立連接到MySQL,用于連接到MySQL 的支持是內(nèi)置在PHP語(yǔ)言中。下面的PHP函數(shù)調(diào)用建立連接:mysql_connect(server_name, username, password); 在這里,地址是IP地址或者電腦的主機(jī)名在MySQL服務(wù)器運(yùn)行( “l(fā)ocalhost”如果是在同一臺(tái)計(jì)算機(jī)上運(yùn)行的Web服務(wù)器軟件)用戶名和密碼是MySQL的用戶名和密碼。當(dāng)被使用時(shí),PHP函數(shù)通常會(huì)返回(輸出)一個(gè)值時(shí),他們被稱為價(jià)值。除了做一些有用的東西時(shí),當(dāng)被使用時(shí),大多數(shù)函數(shù)會(huì)輸出一個(gè)值,這個(gè)值可能存儲(chǔ)在

6、一個(gè)變量中為等下使用。Mysql_connect 函數(shù)顯示以上內(nèi)容,如,返回一個(gè)已經(jīng)被建立的定義了內(nèi)容的數(shù)字。既然我們想利用連接,我們應(yīng)守住這個(gè)值,這里有一個(gè)例子說(shuō)明我們?cè)鯓舆B接我們的MySQL服務(wù)器。$ dbcnx = mysql_connect(“l(fā)ocalhost”,“root” , “mypassed”);如上所述,這三個(gè)函數(shù)參數(shù)的值可能不同于你的MySQL服務(wù)器。重要的是值通過mysql_connect返回存儲(chǔ)在一個(gè)名為$dbcnx的變量中。由于MySQL服務(wù)器是一個(gè)完全獨(dú)立的一塊軟件,我們必須考慮服務(wù)器不可用或無(wú)法訪問由于網(wǎng)絡(luò)中斷,或因?yàn)槟峁┑挠脩裘?密碼組合是不被接受的服務(wù)器的

7、可能性。在這種情況下,mysql_connect函數(shù)沒有返回連接標(biāo)識(shí)符(因?yàn)闆]有建立連接)。相反,它返回false。這允許我們使用if語(yǔ)句來(lái)應(yīng)對(duì)這樣的故障:$dbcnx = mysql_connect("localhost", "root", "mypasswd"); if (!$dbcnx) echo( "<p>Unable to connect to the database server at this time.</p>" ); exit(); 在上面的代碼片段有三個(gè)新方法。首先,我

8、們放置一個(gè)符號(hào)在mysql_connect函數(shù)的前面。很多函數(shù),包括mysql_connect當(dāng)他們失敗時(shí)自動(dòng)顯示難看的錯(cuò)誤消息。放置一個(gè)符號(hào)在函數(shù)名稱前面告訴函數(shù)靜靜的失敗,使我們能夠展示我們自己的,顯示友好的錯(cuò)誤消息。接下來(lái),我們把一個(gè)感嘆號(hào)加在$ dbcnx中變量前面的ifstatement的條件。感嘆號(hào)是PHP拒絕運(yùn)算符,可以翻轉(zhuǎn)錯(cuò)誤轉(zhuǎn)為真,或者真值設(shè)置為錯(cuò)誤 。因此,如果連接失敗,并且mysql_connect返回錯(cuò)誤,!$dbcnx將值為真 ,如果執(zhí)行語(yǔ)句會(huì)導(dǎo)致我們的身體的語(yǔ)句。另外,如果做了一個(gè)連接,存儲(chǔ)在$ dbcnx中的連接標(biāo)識(shí)符的值為真(任何非零數(shù)字被認(rèn)為是“真”在PHP

9、) ,所以! $ dbcnx中的計(jì)算結(jié)果為false,在if語(yǔ)句的語(yǔ)句將不被執(zhí)行。最后的新方法是exit函數(shù),這是我們所遇到的函數(shù)不帶參數(shù)的第一個(gè)例子。所有這個(gè)函數(shù)做的一切是使PHP停止閱讀的頁(yè)面在這一點(diǎn)上。這是一個(gè)失敗的數(shù)據(jù)庫(kù)連接的良好反應(yīng),因?yàn)樵诖蠖鄶?shù)情況下,頁(yè)面將無(wú)法顯示與連接有關(guān)的任何有用信息。下一步,一旦連接建立,則要選擇你要使用的數(shù)據(jù)庫(kù)。比方說(shuō),我們希望與一個(gè)銷售數(shù)據(jù)庫(kù)的工作。我們創(chuàng)建的數(shù)據(jù)庫(kù)名為“銷售” 。選擇該數(shù)據(jù)庫(kù)在PHP中只是調(diào)用一個(gè)函數(shù)。mysql_select_db("sales", $dbcnx); 請(qǐng)注意,我們使用數(shù)據(jù)庫(kù)連接標(biāo)識(shí)符告訴要使用的數(shù)據(jù)

10、庫(kù)連接的函數(shù)$ dbcnx中的變量。這個(gè)參數(shù)實(shí)際上是可選的。當(dāng)它被省略,該函數(shù)會(huì)自動(dòng)連接鏈接標(biāo)識(shí)符打開的最后一個(gè)連接。當(dāng)函數(shù)返回真,說(shuō)明成功,返回失敗,說(shuō)明有錯(cuò)誤。再次提醒審慎的做法是使用if語(yǔ)句來(lái)處理錯(cuò)誤:if (! mysql_select_db( "sales") ) echo( "<p>Unable to locate the sales database at this time.</p>" ); exit(); 注意,這一次,不是分配函數(shù)的結(jié)果給一個(gè)變量然后檢查,如果變量是真的或是假的,我只是使用了函數(shù)調(diào)用自身的條件。

11、這看起來(lái)可能有些奇怪,但它是一個(gè)非常常用的快捷方式。要檢查,如果條件為真或假,PHP執(zhí)行函數(shù)然后檢查它的返回值 這正是我們需要發(fā)生的情況。隨著連接建立和數(shù)據(jù)庫(kù)選擇,我們現(xiàn)在準(zhǔn)備開始使用存儲(chǔ)在數(shù)據(jù)庫(kù)中的數(shù)據(jù)。用PHP發(fā)送SQL查詢?cè)赑HP中,提交查詢到數(shù)據(jù)庫(kù),我們使用mysql_query函數(shù)。mysql_query (query,connection_id);下面的查詢包含我們想要執(zhí)行的SQL命令的字符串。與mysql_select_db ,連接標(biāo)識(shí)符參數(shù)是可選的。函數(shù)返回什么將取決于被發(fā)送的查詢類型。對(duì)于大多數(shù)的SQL命令, mysql_query返回真或假,分別指示成功或失敗。考慮下面的例

12、子,它試圖創(chuàng)建客戶表:$sql = "CREATE TABLE Customers ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Surname Varchar(25), Firstname Varchar(25), Createdate Date )" if ( mysql_query($sql) ) echo("<p>Customers table successfully created!</p>"); else echo("<p>Error creatin

13、g Customers table: " mysql_error() . "</p>"); 同樣,我們使用方法去打壓請(qǐng)求mysql_query所產(chǎn)生的任何錯(cuò)誤信息,而是打印出自己的友好的錯(cuò)誤消息。mysql_error函數(shù)用來(lái)返回一個(gè)字符串文字,描述被送往MySQL服務(wù)器的最后一個(gè)錯(cuò)誤消息。對(duì)于DELETE,INSERT和UPDATE查詢(它用來(lái)修改存儲(chǔ)的數(shù)據(jù) ,MySQL也跟蹤行(條目)都受到了查詢的數(shù)量的影響。考慮下面的SQL命令來(lái)設(shè)置一個(gè)名為“布朗”到“史密斯”所有客戶的姓:$sql = "UPDATE Customers SET Su

14、rname='Smith' WHERE Surname = 'Brown'" When we execute this query, we can use the mysql_affected_rows function to view the number of rows that were affected by this update: if ( mysql_query($sql) ) echo("<p>Update affected " . mysql_affected_rows() " rows.&l

15、t;/p>" ); else echo("<p>Error performing update: " . mysql_error() "</p>" ); SELECT查詢的處理方式不同一點(diǎn),因?yàn)樗麄兛梢垣@取大量的數(shù)據(jù),而PHP必須提供方法來(lái)處理這些信息。處理SELECT結(jié)果集對(duì)于大多數(shù)的SQL查詢,mysql_query函數(shù)返回true(成功)或false (失敗) 。對(duì)于SELECT查詢,這是不夠的。你會(huì)記得SELECT查詢被用來(lái)顯示數(shù)據(jù)庫(kù)中存儲(chǔ)的數(shù)據(jù)。除了指出查詢成功還是失敗,PHP也必須接收的查詢的結(jié)果。因此

16、,當(dāng)它處理一個(gè)SELECT查詢,請(qǐng)求mysql_query返回一個(gè)數(shù)字,用于標(biāo)識(shí)一個(gè)“結(jié)果集”,它包含了所有(條目)從查詢返回的行的列表。如果查詢失敗,仍返回以任何理由的虛假信息。 $result = mysql_query("SELECT Surname FROM Customers"); if (!$result ) echo("<p>Error performing query: " . mysql_error () "</p>"); exit(); 只要沒有錯(cuò)誤在查詢時(shí)遇到,上面的代碼會(huì)放置一

17、個(gè)包含所有存儲(chǔ)在客戶表到變量$ result姓氏的文本結(jié)果集。由于有對(duì)姓氏的數(shù)據(jù)庫(kù)的數(shù)量沒有實(shí)際限制,該結(jié)果集可以是相當(dāng)大的。我們之前提到的while循環(huán)是一個(gè)有用的控制結(jié)構(gòu)來(lái)處理大量數(shù)據(jù)。下面就來(lái)處理行的結(jié)果集一次一個(gè)代碼的輪廓:while ( $row = mysql_fetch_array ($result ) ) / process the row.  $row = mysql_fetch_array($result); ) ;該mysql_fetch_array函數(shù)接受一個(gè)結(jié)果集作為一個(gè)參數(shù)(存儲(chǔ)在這種情況下,$ result變量),并返回結(jié)果集作為數(shù)組的下一行。如果你不熟

18、悉數(shù)組的概念,不用擔(dān)心:我們將在稍后討論它。當(dāng)結(jié)果集中沒有更多的行,而不是mysql_fetch_array返回false 。現(xiàn)在,上面的語(yǔ)句將一個(gè)值賦到$row變量,但在同一時(shí)間整個(gè)聲明本身需要對(duì)相同的值。這就是讓我們使用該語(yǔ)句在while循環(huán)的條件。由于while循環(huán)不斷循環(huán),直到他們的條件計(jì)算為false ,循環(huán)將因?yàn)橛性诮Y(jié)果集中的行,以$行每執(zhí)行一次循環(huán),同時(shí)對(duì)下一行的值發(fā)生多次。所有剩下的就是要弄清楚如何每次得到的值了$ row變量的循環(huán)運(yùn)行。結(jié)果集的行表示為數(shù)組,數(shù)組是一種特殊類型的變量,它包含多個(gè)值。如果你認(rèn)為包含一個(gè)值的變量為一個(gè)箱子,那么數(shù)組可以認(rèn)為是隔室,其中每個(gè)隔室能夠存

19、儲(chǔ)單個(gè)盒子值。在我們的數(shù)據(jù)庫(kù)運(yùn)行的情況下,車廂被命名表中的列后,我們的結(jié)果集$row如果是在我們的結(jié)果集中的行,而$row “ JokeText ”是在JokeText該行的列。因此,我們的while循環(huán)在這里應(yīng)該是這樣,如果我們想打印所有客戶的姓氏在我們的數(shù)據(jù)庫(kù)中:while ( $row = mysql_fetch_array ($result ) ) echo("<p>" . $row "Surname" "</p>"); 總之,這里是一個(gè)PHP的Web頁(yè)面的完整代碼,將連接到我們的數(shù)據(jù)庫(kù),獲取數(shù)據(jù)庫(kù)中所

20、有客戶的姓氏,并以HTML段落顯示出來(lái)。 <html> <head> <title > Our List of Customers </title > <head> <body> <?php / Connect to the database server $dbcnx = mysql_connect( "localhost", "root", "mypasswd"); if (!$dbcnx ) echo( "<p>Unab

21、le to connect to the database server at this time.</p>" ); exit(); / Select the sales database if (! mysql_select_db("sales") ) echo( "<p>Unable to locate the sales database at this time.</p>" ); exit(); ?> <p> Here are all the customers in our dat

22、abase: </p> <blockquote> <?php / Request the surname of all the customers $result = mysql_query ( "SELECT Surname FROM Custom if (!$result) echo("<p>Error performing query: " . mysql_error() "</p>" ) exit(); / Display the surname of each customer i

23、n a paragraph while ( $row = mysql_fetch_array($result ) ) echo("<p>" . $row"Surname" "</p>"); ?> </blockquote> </body> </html> 原文: Database Handling with PHP/MySQL Communication with Databases Unlike other scripting languages for Web pa

24、ge development (i.e. ASP), PHP is open-source, cross-platform, and offers excellent connectivity to most of today's common databases including Oracle, Sybase, Microsoft SQL Server, MySQL, Postgresql, ODBC (and others). PHP also offers integration with various external libraries which enable the

25、developer to do anything from generating PDF documents, accessing secure payment services and producing graphic output, to parsing XML. A Look Back at First Principles Before we leap forward, it's worth a brief look back to remind ourselves of the goal we're working toward. We have two power

26、ful, new tool s at our disposal: the PHP scripting language, and the MySQL database engine. It's important to understand how these two will fit together. The whole idea of a database-driven Web site is to allow the content of the site to reside in a database, and for that content to be dynamical

27、ly pulled from the database to create Web pages for people to view with a regular Web browser. So on one end of the system you have a visitor to your site who uses a Web browser to load , and expects to view a standardHTML Web page. On the other end you have the content of your site, part of which s

28、its in one ormore tables in a MySQL database that only understands how to respond to SQL queries (commands). Tthe PHP scripting language is the go-between that speaks both languages. It processes the page request and fetches the data from the MySQL database, then spits it out dynamically as the nice

29、ly-formatted HTML page that the browser expects. With PHP, you can write the presen tation aspects of your site (the fancy graphics and page layouts) as "templates" in regular HTML. Where the content belongs in those templates, you use some PHP code to connect to the MySQL database and - u

30、sing SQL queries retrieve and display some content in its place. Just so it's clear and fresh in your mind, this is what will happen when someone visits a page on our database-driven Web site: · The visitor's Web browser requests the Web page using a standard URL · The Web server s

31、oftware (Apache, IIS, or wh atever) recognizes that the requested file is a PHP script, and so the server interprets the file using its PHP plug -in, before responding to the page request. · Certain PHP commands (which we have yet to learn) connect to the MySQL database and request the content

32、that belongs in the Web page. · The MySQL database responds by sending the requested content to the PHP script. · The PHP script stores the content into one or more PHP variables, and then uses the now-familiar echo function to output the content as part of the Web page. · The PHP plu

33、g-in finishes up by handing a copy of the HTML it has created to the Web server. · The Web server sends the HTML to the Web browser as it would a plain HTML file, except that instead of coming directly from an HTML file, the page is the output provided by the PHP plug-in. Connecting to MySQLwit

34、h PHP Before you can get content out of your MySQL database for inclusion in a Web page, you must first know how to establish a connection to MySQL from inside a PHP script. PHP support for connecting to MySQL is built right into the language. The following PHP function call establishes the connecti

35、on: mysql_connect(server_name, username, password); Here, address is the IP address or hostname of the computer on which the MySQL server software is running ("localhost" if it's running on the same computer as the Web server software), and username and password are the MySQL user name

36、 and password. Functions in PHP usually return (output) a value when they are called. In addition to doing something useful when they are called, most functions output a value, and that value may be stored in a variable for later use. The mysql_connect function shown above, for example, returns a nu

37、mber that identifies the connection that has been established. Since we intend to make use of the connection, we should hold onto this value. Here's an example of how we might connect to our MySQL server. $dbcnx = mysql_connect("localhost", "root", "mypasswd"); As d

38、escribed above, the values of the three function pa rameters may differ for your MySQL server. What's important to see here is that the value returned by mysql_connect (which we'll call a connection identifier) is stored in a variable named $dbcnx. Since the MySQL server is a completely sepa

39、rate piece of s oftware, we must consider the possibility that the server is unavailable, or inaccessible due to a network outage, or because the username/password combination you provided is not accepted by the server. In such cases, the mysql_connect function doesn't return a connection identi

40、fier (since no connection is established). Instead, it returns false. This allows us to react to such failures using an if statement: $dbcnx = mysql_connect("localhost", "root", "mypasswd"); if (!$dbcnx) echo( "<p>Unable to connect to the database server at t

41、his time.</p>" ); exit(); There are three new tricks in the above code fragment. First, we have placed an symbol in front of the mysql_connect function. Many functions, including mysql_connect, automatically display ugly error messages when they fail. Placing an symbol in front of the fun

42、ction name tells the function to fail silently, allowing us to display our own, friendlier error message. Next, we put an exclamation point in front of the $dbcnx variable in the condition of the ifstatement. The exclamation point is the PHP negation operator, which basically flips a false value to

43、true, or a true value to false. Thus, if the connection fails and mysql_connect returns false, !$dbcnx will evaluate to true, and cause the statements in the body of our if statement to be executed. Alternatively, if a connection was made, the connection identifier stored in $dbcnx will evaluate to

44、true (any number other than zero is considered "true" in PHP), so !$dbcnx will evaluate to false, and the statements in the if statement will not be executed. The last new trick is the exit function, which is the first example that we've encountered of a function that takes no paramete

45、rs. All this function does is cause PHP to stop reading the page at this point. This is a good response to a failed database connection, because in most cases the page will be unable to display any useful information without that connection. The next step, once a connection is established, is to sel

46、ect the database you want to work with. Let's say we want to work with a sales database. The database we created is called “sales”. Selecting that database in PHP is just a matter of another function call: mysql_select_db("sales", $dbcnx); Notice we use the $dbcnx variable that contain

47、s the database connection identifier to tell the function which database connection to use. This parameter is actually optional. When it's omitted, the function will automatically use the link identifier for the last connection opened. This function returns true when it's successful and fals

48、e if an error occurs. Once again, it's prudent to use an if statement to handle errors: if (! mysql_select_db( "sales") ) echo( "<p>Unable to locate the sales database at this time.</p>" ); exit(); Notice that this time, instead of assigning the result of the func

49、tion to a variable and then checking if the variable is true or false, I have simply used the function call itself as the condition. This may look a little strange, but it's a very commonly used shortcut. To check if the condition is true or false, PHP executes the function and then checks its r

50、eturn value - exactly what we need to happen. With a connection established and a database selected, we are now ready to begin using the data stored in the database. Sending SQL Queries with PHP In PHP, to submit a query to the d atabase, we use the mysql_query function. mysql_query(query, connectio

51、n_id ); Here query is a string that contains the SQL command we want to execute. As with mysql_select_db, the connection identifier parameter is optional. What this function returns will depend on the type of query being sent. For most SQL commands, mysql_query returns either true or false to indica

52、te success or failure respectively. Consider the following example, which attempt to create the customers table: $sql = "CREATE TABLE Customers ( ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, Surname Varchar(25), Firstname Varchar(25), Createdate Date )" if ( mysql_query($sql) ) echo("&

53、lt;p>Customers table successfully created!</p>"); else echo("<p>Error creating Customers table: " mysql_error() . "</p>"); Again, we use the trick to suppress any error messages produced by mysql_query, and instead print out a friendlier error message of

54、 our own. The mysql_error function used here returns a string of text that describes the last error message that was sent by the MySQL server. For DELETE, INSERT, and UPDATE queries (which serve to modify stored data), MySQL also keeps track of the number of table rows (entries) that were affected b

55、y the query. Consider the SQL command below to set the surname of all customers named “Brown” to “Smith”: $sql = "UPDATE Customers SET Surname='Smith' WHERE Surname = 'Brown'" When we execute this query, we can use the mysql_affected_rows function to view the number of rows

56、 that were affected by this update: if ( mysql_query($sql) ) echo("<p>Update affected " . mysql_affected_rows() " rows.</p>" ); else echo("<p>Error performing update: " . mysql_error() "</p>" ); SELECT queries are treated a little diffe

57、rently, since they can retrieve a lot of data, and PHP must provide ways to handle that information.Handling SELECT Result Sets For most SQL queries, the mysql_query function returns either true (success) or false (failure). For SELECT queries this just isn't enough. You'll recall that SELEC

58、T queries are used to view stored data in the database. In addition to indicating whether the query succeeded or failed, PHP must also receive the results of the query. As a result, when it processes a SELECT query, mysql_query returns a number that identifies a "result set", which contain

59、s a list of all the rows (entries) returned from the query. False is still returned if the query fails for any reason. $result = mysql_query("SELECT Surname FROM Customers"); if (!$result ) echo("<p>Error performing query: " . mysql_error () "</p>"); exit(); Provided no error was encountered in processing the query, the above code will place a result set that contains the text of all the surnames sto

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論