PHP与MySQL信息互联【外文翻译】

PHP与MySQL信息互联【外文翻译】
PHP与MySQL信息互联【外文翻译】

外文翻译

外文文献译文

标题:PHP与MySQL信息互联

资料来源: PHP和MySQL Web开发作者:C.奥尔

现在你已经可以熟练地使用MySQL客户端软件来操作数据库里的数据,我们也可以开始学习如何使用PHP来显示和修改数据库里的数据了。PHP有标准的函数用来操作数据库。我们首先学习PHP内建的数据库函数,然后会学习PHP扩展和应用程序库(PEAR,PHP Extension and Application Repository )中的数据库函数,我们可以使用这些函数操作所有支持的数据库。这种灵活性源自于抽象。对于编程接口而言,抽象简化了复杂的交互过程。它将交互过程中无关紧要的部分屏蔽起来,让你关注于重要的部分。PEAR的DB类就是这样一种数据库接口的抽象。你登录一个数据库所需要提供的信息被减少到最少。这种标准的格式可以通过同一个函数来访问MySQL以及其他的数据库。同样,一些MySQL特定的函数被更一般的、可以用在很多数据库上的函数所替代。

在使用MySQL的接口作为PHP的原因是格式化的形式在网页中可见的SQL查询的结果。只要你可以登录到您的MySQL-stallation使用用户名和密码,你也可以这样做,从PHP。然而,而不是使用MySQL的命令行来输入指令,查看输出,您将创建传递给MySQL查询字符串。当MySQL返回其响应它会作为一种数据结构,而不是PHP可以识别的格式的输出,你看当你在命令行工作。此外PHP命令可以检索的网页数据和格式。

用PHP开发的网站大多包含多个程序文件,需要访问MySQL,因此将需要登录名和密码的细节。因此,明智的做法建立一个单一的文件来存储这些,然后包括,凡是有需要的文件。例10-1显示了这样一个文件,这是我打过电话login.php。输入自己,重新把值与实际值你为你的MySQL大tabase使用,(如用户名)保存到Web 开发目录设置在第2章注册。我们将利用短期内的文件的使用。主机名localhost应该工作,只要你使用本地系统上的一个MySQL数据库,该数据库出版物应的工作,如果你在我使用至今的例子打字。

现在,你有login.php文件保存,你可以在任何PHP文件,将需要访问使用require_once语句的数据库中。这已被选定优先于include语句,因为它会产生一个致命错误,如果该文件不发现。相信我,没有找到该文件包含到你的数据库登陆信息是一个致命的错误。此外,而不是使require_once要求该文件将被读取意味着只有当它以前没有被列入,防止重复浪费的磁盘访问。

首先,将变量设置为$查询要作出查询。在这种情况下,要见的经典表中的所有行。请注意,与采用MySQL的命令行,没有分号,在查询的尾巴必需的,因为在mysql_query函数用于发出一个完整的查询,并不能用来发送多个部分,一来查询一次。因此,mysql知道查询是否完整,不查找一个分号。这个函数返回一个结果,我们在变量$结果的地方。经使用MySQL在命令行,你可能认为导致美元的内容,作一个命令行查询与水平和垂直线,返回的结果相同,等等。但是,这不是与PHP的结果返回情况。相反,如果成功,$结果将包含一个可用于提取查询结果的资源。你将看到如何在下一节中提取的数据。如果失败,$结果包含虚假。因此,例如通过检查$完成的结果。如果是false,它意味着有一个错误,并且死命令被执行。一旦你有一个资源从一个请求mysql_query函数返回的,你可以用它来检索所需的数据。最简单的方式做,这是你想要提取的细胞,一次一个,使用mysql_result功能。示例10-5结合,并延伸到一个程序前面的例子,你可以输入并运行自己的检索返回结果。我建议你保存在同一文件夹作为login.php,并给它的名字query.php。

它首先检查任何可能已输入,然后要么插入到经典的出版物数据库表的新数据或删除了一排,根据在输入供应。不管是否有输入,然后输出该方案在向浏览器表的所有行。所以让我们看看它是如何工作。新代码的第一部分开始使用使用isset函数来检查是否所有字段的值已被张贴到该程序。当这种确认,第一个六行的if语句在每个调用函数get_post,这是在节目的最后出现。这个函数有一个小而关键的工作:从浏览器获取输入。

我刚才在前面的章节,浏览器的用户输入或者通过发送一个GET请求或POST 请求。POST请求通常是首选,我们这里使用它。该网站的注册用户输入的所有(即使形式是充满了百场出)并将其放入到一个数组服务器捆绑命名的$ _POST的

$ _POST是一个关联数组。根据表单是否已被设置为使用POST或GET方法,要么了$ _POST或$_GET这个关联数组的形式将与数据填充。他们都在读完全相同的方式。每个领域都有一个名为后该领域的数组元素。因此,如果一个表单包含名为ISBN 的领域,$_POST数组包含了字书号键的元素。 PHP的程序可以参照阅读$ _POST

或['书号']或领域的$ _POST[“书号“](单引号和双引号在这种情况下有相同的效果)。如果变量$ _POST复杂的语法似乎仍然对你放心,你可以只使用了我在例10-8所示公约,复制用户的输入到其他变数,而忘记后,大约的$ _POST。这是正常的PHP 程序:它们检索在程序开始的$ _POST所有的字段,然后忽略它。我们没有理由写入一个在$_POST数组元素。它的唯一目的是交流信息,从浏览器亲克,你最好修改之前复制到你自己的变量数据。

所以,回到get_post功能,穿过mysql_real_escape_string函数每个项目剥离出它检索到的任何字符,一个黑客可能已插入,以打破或改变成一个记录您的database.Deleting经装了,本来是可以与任何人通过各种可能的变量值张贴,然后程

序检查是否变量的$ _POST['删除']有值。如果是这样,用户点击了一个删除记录buttonto删除一个记录。在这种情况下,美元的书号也将被posted.As你还记得值,书号唯一标识每个记录。 HTML表单追加到删除从查询字符串变量$查询,然后将其传递给在mysql_query函数创建到MySQL的问题,它的ISBN。请求mysql_query返回TRUE或FALSE,假导致错误消息将显示解释了什么wrong.If$删除不包含单词“是”,那么下面的else语句被执行。$查询设置为一个INSERT INTO命令,随后的五要插入的值。该变量然后传递到请求mysql_query,而于完工后返回TRUE或FALSE。如果返回FALSE,错误信息显示。

部分简单的HTML表单设置形式的行动,sqltest.php。这意味着,当表单被提交的表单字段的内容将被发送到文件sqltest.php,这是程序本身。该表格也成立一个POST发送的字段,而不是一个GET请求。这是因为GET请求追加到URL提交和自己能看在你的浏览器凌乱。它们还允许用户方便地修改意见,并尝试破解你的服务器。因此,只要有可能,你应该使用POST提交的,其中也有隐藏的发布从视图数据的好处。有输出的表单域,一个提交的HTML显示的名称添加记录按钮,并关闭窗体。请注意

和““标签在这里,这已被用于强制等宽字体,让所有的输入排队整齐的使用。在每一行的末尾回车输出时也
标签内。

现在让我们来看看如何删除一个记录,创造一个虚拟的纪录作品。因此,尝试进入的五个领域和添加记录按钮,按一下每个只是数字1。如果你现在向下滚动,你会看到一个新的纪录仅仅1秒组成。显然,这是记录在此表非常有用,所以现在按一下按钮,滚动删除记录下来了,以确认该记录已被删除。假设一切正常,你现在可以随意添加和删除记录。尝试这样做了几次,但留在原地(包括白鲸新一)的主要记载,我们将使用它们。您也可以尝试加入1的纪录再次与所有有几次,并记下错误信息,您将收到第二次,在- dicating已经有一个数字1的ISBN。您现在已经准备好一些实用技术,您可以在使用PHP来访问MySQL数据库,包括创建和删除表,在任务查找serting,更新和删除数据,保护用户免受恶意您的数据库和网站。请注意下面的例子假设你已经创建了login.php节目讨论本章前面。假设你是一个野生动物公园的工作,需要建立一个数据库,保存一切有关它的猫房子类型的详细信息。你是说有九个家庭的猫:狮子,老虎,美洲豹,豹,美洲狮,猎豹,猞猁,狞猫,并与国内,所以你需要一个该列。然后每个猫被赋予一个名字,所以这是另一列,而你也想了解他们的年龄,这是另一种轨道。当然,你可能会需要更多的列后,也许是为了保持膳食重新quirements,接种,以及其他细节,但现在这足够走了。一个独特的标识,也需要每个动物,所以你也决定建立该叫做id列。

PHP包括与核心建立自由和开放的源代码库。 PHP是一个模块,从根本上互联网的感知系统建立访问FTP服务器,数据库服务器中的许多,嵌入式SQL等嵌入式PostgreSQL,MySQL和SQLite的,LDAP服务器和其他图书馆。熟悉的,如在stdio

的家人的C程序员的许多功能都可以在标准的PHP版本。 PHP具有传统上采用“magic_quotes_gpc设置“和”magic_quotes_run time“企图逃避撇号(')和假设字符串在引号(“)表示,他们将在数据库中使用,以防止SQL注入攻击的功能。这将导致混乱以上的数据是逃跑了,这是不是和数据时的问题作为输入到数据库并使用时,其实不是逃避使用的是不完全正确的。[68]使服务器之间的代码是什么,不使用便携魔术引号,开发人员可以用一个脚本来前言扭转魔术引号的作用是当其用于他们的代码。PHP允许开发者编写C扩展功能添加到PHP语言。这些可以被编译成PHP或在运行时动态加载。扩展已被写入添加的Windows API,工艺类Unix操作系统,多字节字符串(Unicode)的,curl和几种流行的压缩格式管理支持。一些更不寻常的功能包括互联网中继聊天,动态生成的图像和Adobe Flash内容,以及甚至语音合成相结合。PHP扩展社区库(PECL电平)项目是在PHP语言的扩展的储存库。Zend 提供了程序员认证,成为PHP开发认证考试。

外文文献原文

Title: interconnect technology of php and mysql

Material Source: Web Development Of PHP and MySQL

Author: C.Orr

Now that you’re comfortable 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 built-in database functions. We’ll also show you how to use the The PHP Extension and Application Repository (PEAR) databaseunctions that provide the ability to use the same functions to access any supported database. This type of flexibility comes from a process called abstraction. In programming interfaces, abstraction simplifies a complex interaction. It works by

removing any nonessential parts of the interaction, allowing you to concentrate on the important parts. PEAR’s DB class es 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 MySQL-specific functions are replaced with generic ones that know how to talk to many databases.

The reason for using PHP as an interface to MySQL is to format the results of SQL queries in a form visible in a web page. As long as you can log into your MySQL in- stallation using your username and password, you can also do so from PHP. However, instead of using MySQL’s command line to enter instructions and view output, you will create query strings that are passed to MySQL. When MySQL returns its response,it will come as a data structure that PHP can recognize instead of the formatted output you see when you work on the command line. Further PHP commands can retrieve the data and format it for the web page.

Most websites developed with PHP contain multiple program files that will require access to MySQL and will therefore need the login and password details. Therefore, it’s sensible to create a single file to store these and then include that file wherever it’s needed. Example 10-1 shows such a file, which I’ve called login.php. Type it in, re- placing values (such as username) with the actual values you use for your MySQL da- tabase, and save it to the web development directory

you set up in Chapter 2. We’ll be making use of the file shortly. The hos tname localhost should work as long as you’re using a MySQL database on your local system, and the database publications should work if you’re typing in the examples I’ve used so far.

Now that you have the login.php file saved, you can include it in any PHP files that will need to access the database by using the require_once statement. This has been chosenin preference to an include statement, as it will generate a fatal error if the file is notfound. And believe me, not finding the file containing the login details to your databaseis a fatal error.Also, using require_once instead of require means that the file will be read in only whenit has not previously been included, which prevents wasteful duplicate disk accesses. Example 10-2 shows the code to use.

First, the variable $query is set to the query to be made. In this case it is asking to see all rows in the table classics.Note that, unlike using MySQL’s command line, no semicolon is required at the tail of the query, because the mysql_query function is usedto issue a complete query, and cannot be used to query by sending multiple parts, oneat a time. Therefore, MySQL knows the query is complete and doesn’t look for a semicolon.This function returns a result that we place in the variable $result. Having used MySQL.at the command line, you might think that the contents of $result will be the same as the result returned from a command-line query, with horizontal and vertical lines, and so on. However, this is not the case with the result returned to PHP. Instead, upon success, $result will contain a resource that can be used to extract the results of the query. You’ll see how to extract the data in the next section. Upon failure, $result contains FALSE. So the example finishes by checking $result. If it’s F ALSE, it means that there was an error and the die command is executed. Once you have a resource returned from a mysql_query function, you can use it to retrieve the data you want. The simplest way to do this is to fetch the cells you want, one at a time, using the mysql_result function. Example 10-5 combines and extends the previous examples into a program that you can type in and run yourself to retrieve the returned results. I suggest that you save it in the same folder as login.php and give it the name query.php.

It first checks for any inputs that may have been made and then either inserts new data into the classics table of the publications database or deletes a row from it, according to the input supplied. Regardless of whether there was input, the program then outputs all rows in the table to the browser. So let’s see how it works.The first section of new code starts by using the isset function to check whether values for all

相关主题
相关文档
最新文档