PHP毕业设计(论文)外文翻译_第1页
PHP毕业设计(论文)外文翻译_第2页
PHP毕业设计(论文)外文翻译_第3页
PHP毕业设计(论文)外文翻译_第4页
PHP毕业设计(论文)外文翻译_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

英文原文名 Database Handling With PHP/MySQL中文译名 数据库处理PHP/MySQL 英文原文版出处:(美)Chris Shiflett, Marcus Baker PHP in Action M.2010-01-01:361-430译文成绩: 指导教师(导师组长)签名: 译文:与数据库通信不同其他脚本语言的Web页面开发,即ASP ,PHP是开源的,跨平台的,并提供良好的连接,今天的大多数常见的数据库,包括Oracle , Sybase的Microsoft SQL Server ,MySQL和PostgreSQL , ODBC 。 PHP还提供了集成各种外部库,使开发人员能够生成PDF文档,访问安全的支付服务,并产生图形输出以解析XML 。回头看看第一原理在我们的飞跃的同时要简短的回顾一下,去提醒我们正在努力朝着自己的目标。我们有两个强大的,新的工具的在我们的面前:PHP脚本语言和MySQL数据库引擎。重要的是要了解这两个怎么会结合在一起。一个数据库驱动的Web站点的整个想法是让网站的内容驻留在数据库中,并从数据库中动态地抽取数据来创建网页,来让人用一个普通的web浏览器查看。因此,在系统的一端你有一个访问者到你的网站使用Web浏览器来加载 ,期望看到一个standardHTML网页。在另一端你有你的网站,其中一部分在一个MySQL数据库中的一个或以上的表,只有懂得何为SQL查询(命令)才能作出回应的内容。 PHP脚本语言是穿针引线于两种语言之间,它处理页面请求,并获取从MySQL数据库中的数据,然后动态地得出浏览器期望得到的格式化的HTML页面。使用PHP,你可以写网站(花哨的图形和页面布局)像常规HTML的 “模板” 。其中内容属于这些模板,你可以使用一些PHP代码来连接MySQL数据库和 - 使用SQL查询检索并显示在它地方的一些内容。当有人访问我们的数据库驱动的Web站点的页面会发生什么这是很清楚和明显的:使用标准的URL访问者的Web浏览器请求的网页 Web服务器软件( Apache的,IIS或WH atever )认识到所需的文件是一个PHP脚本,所以使用它的PHP插件,响应页面请求之前,服务器解释该文件。某些PHP命令(我们还没有学会)连接到MySQL数据库,并要求所属在Web页面的内容。 MySQL数据库通过发送所请求的内容到PHP脚本。 PHP脚本存储内容到一个或多个PHP变量,然后使用现在,熟悉的echo函数来输出内容作为网页的一部分。 PHP的插件,通过分发它创造到Web服务器的HTML副本结束了。 Web服务器发送的HTML到Web浏览器,因为它会一个纯HTML文件,不同之处在于,而不是直接从HTML文件来了,该网页是由PHP插件提供的输出。用PHP连接到MySQL在你可以得到的内容用来提供载入网页的MySQL数据库之前,你必须知道如何从一个PHP脚本中建立连接到MySQL,用于连接到MySQL 的支持是内置在PHP语言中。下面的PHP函数调用建立连接:mysql_connect(server_name, username, password); 在这里,地址是IP地址或者电脑的主机名在MySQL服务器运行( “localhost”如果是在同一台计算机上运行的Web服务器软件)用户名和密码是MySQL的用户名和密码。当被使用时,PHP函数通常会返回(输出)一个值时,他们被称为价值。除了做一些有用的东西时,当被使用时,大多数函数会输出一个值,这个值可能存储在一个变量中为等下使用。Mysql_connect 函数显示以上内容,如,返回一个已经被建立的定义了内容的数字。既然我们想利用连接,我们应守住这个值,这里有一个例子说明我们怎样连接我们的MySQL服务器。$ dbcnx = mysql_connect(“localhost”,“root” , “mypassed”);如上所述,这三个函数参数的值可能不同于你的MySQL服务器。重要的是值通过mysql_connect返回存储在一个名为$dbcnx的变量中。由于MySQL服务器是一个完全独立的一块软件,我们必须考虑服务器不可用或无法访问由于网络中断,或因为您提供的用户名/密码组合是不被接受的服务器的可能性。在这种情况下,mysql_connect函数没有返回连接标识符(因为没有建立连接)。相反,它返回false。这允许我们使用if语句来应对这样的故障:$dbcnx = mysql_connect(localhost, root, mypasswd); if (!$dbcnx) echo( Unable to connect to the database server at this time. ); exit(); 在上面的代码片段有三个新方法。首先,我们放置一个符号在mysql_connect函数的前面。很多函数,包括mysql_connect当他们失败时自动显示难看的错误消息。放置一个符号在函数名称前面告诉函数静静的失败,使我们能够展示我们自己的,显示友好的错误消息。接下来,我们把一个感叹号加在$ dbcnx中变量前面的ifstatement的条件。感叹号是PHP拒绝运算符,可以翻转错误转为真,或者真值设置为错误 。因此,如果连接失败,并且mysql_connect返回错误,!$dbcnx将值为真 ,如果执行语句会导致我们的身体的语句。另外,如果做了一个连接,存储在$ dbcnx中的连接标识符的值为真(任何非零数字被认为是“真”在PHP ) ,所以! $ dbcnx中的计算结果为false,在if语句的语句将不被执行。最后的新方法是exit函数,这是我们所遇到的函数不带参数的第一个例子。所有这个函数做的一切是使PHP停止阅读的页面在这一点上。这是一个失败的数据库连接的良好反应,因为在大多数情况下,页面将无法显示与连接有关的任何有用信息。下一步,一旦连接建立,则要选择你要使用的数据库。比方说,我们希望与一个销售数据库的工作。我们创建的数据库名为“销售” 。选择该数据库在PHP中只是调用一个函数。mysql_select_db(sales, $dbcnx); 请注意,我们使用数据库连接标识符告诉要使用的数据库连接的函数$ dbcnx中的变量。这个参数实际上是可选的。当它被省略,该函数会自动连接链接标识符打开的最后一个连接。当函数返回真,说明成功,返回失败,说明有错误。再次提醒审慎的做法是使用if语句来处理错误:if (! mysql_select_db( sales) ) echo( Unable to locate the sales database at this time. ); exit(); 注意,这一次,不是分配函数的结果给一个变量然后检查,如果变量是真的或是假的,我只是使用了函数调用自身的条件。这看起来可能有些奇怪,但它是一个非常常用的快捷方式。要检查,如果条件为真或假,PHP执行函数然后检查它的返回值 这正是我们需要发生的情况。随着连接建立和数据库选择,我们现在准备开始使用存储在数据库中的数据。用PHP发送SQL查询在PHP中,提交查询到数据库,我们使用mysql_query函数。mysql_query (query,connection_id);下面的查询包含我们想要执行的SQL命令的字符串。与mysql_select_db ,连接标识符参数是可选的。函数返回什么将取决于被发送的查询类型。对于大多数的SQL命令, mysql_query返回真或假,分别指示成功或失败。考虑下面的例子,它试图创建客户表:$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(Customers table successfully created!); else echo(Error creating Customers table: mysql_error() . ); 同样,我们使用方法去打压请求mysql_query所产生的任何错误信息,而是打印出自己的友好的错误消息。mysql_error函数用来返回一个字符串文字,描述被送往MySQL服务器的最后一个错误消息。对于DELETE,INSERT和UPDATE查询(它用来修改存储的数据 ,MySQL也跟踪行(条目)都受到了查询的数量的影响。考虑下面的SQL命令来设置一个名为“布朗”到“史密斯”所有客户的姓:$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 that were affected by this update: if ( mysql_query($sql) ) echo(Update affected . mysql_affected_rows() rows. ); else echo(Error performing update: . mysql_error() ); SELECT查询的处理方式不同一点,因为他们可以获取大量的数据,而PHP必须提供方法来处理这些信息。处理SELECT结果集对于大多数的SQL查询,mysql_query函数返回true(成功)或false (失败) 。对于SELECT查询,这是不够的。你会记得SELECT查询被用来显示数据库中存储的数据。除了指出查询成功还是失败,PHP也必须接收的查询的结果。因此,当它处理一个SELECT查询,请求mysql_query返回一个数字,用于标识一个“结果集”,它包含了所有(条目)从查询返回的行的列表。如果查询失败,仍返回以任何理由的虚假信息。$result = mysql_query(SELECT Surname FROM Customers); if (!$result ) echo(Error performing query: . mysql_error () ); exit(); 只要没有错误在查询时遇到,上面的代码会放置一个包含所有存储在客户表到变量$ result姓氏的文本结果集。由于有对姓氏的数据库的数量没有实际限制,该结果集可以是相当大的。我们之前提到的while循环是一个有用的控制结构来处理大量数据。下面就来处理行的结果集一次一个代码的轮廓:while ( $row = mysql_fetch_array ($result ) ) / process the row. $row = mysql_fetch_array($result); ) ;该mysql_fetch_array函数接受一个结果集作为一个参数(存储在这种情况下,$ result变量),并返回结果集作为数组的下一行。如果你不熟悉数组的概念,不用担心:我们将在稍后讨论它。当结果集中没有更多的行,而不是mysql_fetch_array返回false 。现在,上面的语句将一个值赋到$row变量,但在同一时间整个声明本身需要对相同的值。这就是让我们使用该语句在while循环的条件。由于while循环不断循环,直到他们的条件计算为false ,循环将因为有在结果集中的行,以$行每执行一次循环,同时对下一行的值发生多次。所有剩下的就是要弄清楚如何每次得到的值了$ row变量的循环运行。结果集的行表示为数组,数组是一种特殊类型的变量,它包含多个值。如果你认为包含一个值的变量为一个箱子,那么数组可以认为是隔室,其中每个隔室能够存储单个盒子值。在我们的数据库运行的情况下,车厢被命名表中的列后,我们的结果集$row如果是在我们的结果集中的行,而$row “ JokeText ”是在JokeText该行的列。因此,我们的while循环在这里应该是这样,如果我们想打印所有客户的姓氏在我们的数据库中:while ( $row = mysql_fetch_array ($result ) ) echo( . $row Surname ); 总之,这里是一个PHP的Web页面的完整代码,将连接到我们的数据库,获取数据库中所有客户的姓氏,并以HTML段落显示出来。 Our List of Customers ?php / Connect to the database server $dbcnx = mysql_connect( localhost, root, mypasswd); if (!$dbcnx ) echo( Unable to connect to the database server at this time. ); exit(); / Select the sales database if (! mysql_select_db(sales) ) echo( Unable to locate the sales database at this time. ); exit(); ? Here are all the customers in our database: ?php / Request the surname of all the customers $result = mysql_query ( SELECT Surname FROM Custom if (!$result) echo(Error performing query: . mysql_error() ) exit(); / Display the surname of each customer in a paragraph while ( $row = mysql_fetch_array($result ) ) echo( . $rowSurname ); ? 原文: Database Handling with PHP/MySQL Communication with Databases Unlike other scripting languages for Web page development (i.e. ASP), PHP is open-source, cross-platform, and offers excellent connectivity to most of todays common databases including Oracle, Sybase, Microsoft SQL Server, MySQL, Postgresql, ODBC (and others). PHP also offers integration with various external libraries which enable the 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, its worth a brief look back to remind ourselves of the goal were working toward. We have two powerful, new tool s at our disposal: the PHP scripting language, and the MySQL database engine. Its 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 dynamically 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 sits 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 nicely-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 - using SQL queries retrieve and display some content in its place. Just so its clear and fresh in your mind, this is what will happen when someone visits a page on our database-driven Web site: The visitors Web browser requests the Web page using a standard URL The Web server software (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 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 plug-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 MySQLwith 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 connection: 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 its running on the same computer as the Web server software), and username and password are the MySQL user name 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 number that identifies the connection that has been established. Since we intend to make use of the connection, we should hold onto this value. Heres an example of how we might connect to our MySQL server. $dbcnx = mysql_connect(localhost, root, mypasswd); As described above, the values of the three function pa rameters may differ for your MySQL server. Whats important to see here is that the value returned by mysql_connect (which well call a connection identifier) is stored in a variable named $dbcnx. Since the MySQL server is a completely separate 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 doesnt return a connection identifier (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( Unable to connect to the database server at this time. ); 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 function 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 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 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 weve encountered of a function that takes no parameters. 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 select the database you want to work with. Lets 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 contains the database connection identifier to tell the function which database connection to use. This parameter is actually optional. When its omitted, the function will automatically use the link identifier for the last connection opened. This function returns true when its successful and false if an error occurs. Once again, its prudent to use an if statement to handle errors: if (! mysql_select_db( sales) ) echo( Unable to locate the sales database at this time. ); exit(); Notice that this time, instead of assigning the result of the function 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 its a very commonly used shortcut. To check if the condition is true or false, PHP executes the function and then checks its return 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, connection_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 indicate 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(Customers table successfully created!); else echo(Error creating Customers table: mysql_error() . ); Again, we use the trick to suppress any error messages produced by mysql_query, and instead print out a friendlier error message of 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 by 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 that were affected by this update: if ( mysql_query($sql) ) echo(Update affected . mysql_affected_rows() rows. ); else echo(Error performing update: . mysql_error() ); SELECT queries are treated a little differently, 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 isnt enough. Youll recall that SELECT 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 contains 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(Error performing query: . mysql_error () ); 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 stored in the Customers table into the variable $result. As theres no practical limit on the number of surnames

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论