用PHP编写自己MySQL连接

While there are advantages to using point-and-click tools such as DreamWeaver to create database connectivity on a web page, doing so separates limits customization choices while maximizing the number of steps required to get results (“where was that option in the menu again?”). Generally speaking, hand coding a database connection is faster and creates more reliable code using fewer lines. It also allows you to actually understand what you are doing, rather than pushing buttons.

尽管使用诸如DreamWeaver之类的点击工具在网页上创建数据库连接具有很多优势,但这样做可以在最大程度地获得结果所需的步骤数的同时,限制了自定义选项的选择(“菜单中的该选项再次出现在哪里” ?”)。 一般而言,手动编码数据库连接速度更快,并且使用更少的行即可创建更可靠的代码。 它还使您能够真正了解自己在做什么,而不是按按钮。

In the steps that follow, I’m assuming that you have a MySQL server, and have created a database table with information inside it.

在接下来的步骤中,我假设您有一台MySQL服务器,并创建了一个数据库表,其中包含信息

First, we are going to write this script on a single page, the same page on which we are going to show database results. While this isn’t terribly efficient for a site in which multiple pages draw information from a database, it does avoid the issue that I discussed in Creating a MySQL Connection In DreamWeaver: that of leaving your connection information “in the clear” as an unsecured include file for anyone to find.

首先,我们将在单个页面上编写此脚本,该页面将在同一页面上显示数据库结果。 虽然这对于其中多个页面从数据库中获取信息的站点而言并不是很有效,但它确实避免了我在DreamWeaver创建MySQL连接中讨论的问题:将连接信息“明确”为不安全的问题include文件供任何人查找。

Obviously, we must establish our database connection, draw information from a table, and translate it into data that we can use before we use the information anywhere on our web page: for that reason, I usually write the connection script at the very start of the page.

显然,我们必须建立数据库连接,从表中获取信息,并将其转换为可使用的数据, 然后再在网页上的任何位置使用该信息:由于这个原因,我通常在这一页。

The first line of PHP code looks like this. (Note that I’m using the short version of the opening PHP tag to save a little space).

PHP代码的第一行如下所示。 (请注意,我使用的是开放PHP标记的简短版本,以节省一些空间)。

<? $myDatabase = mysql_connect("mysql_server", "user", "password") 
	or trigger_error(mysql_error(),E_USER_ERROR); ?>

This establishes a connection to your MySQL server, returning an error if no connection can be made. (Of course, you’d replace mysql_server, user and password with the appropriate information.)

这将建立与MySQL服务器的连接,如果无法建立连接,则返回错误。 (当然,您将使用适当的信息替换mysql_serveruserpassword 。)

The next line chooses the database you wish to draw information from. Again, you’d replace database with the actual name of your database.

下一行选择您希望从中提取信息的数据库。 同样,您将用database的实际名称替换数据库。

<? mysql_select_db("database", $myDatabase); ?>

It is the two lines above that would typically be used in an include, as we would require every page that interacted with a database to have them. The lines that follow would usually be unique to each page.

上面的两行通常用于include ,因为我们需要与数据库进行交互的每个页面都具有它们。 接下来的行通常对于每个页面都是唯一的。

<? $query = "SELECT * FROM table"; ?>

Once working with MySQL becomes familiar, you’ll find that you will spend most of your time tweaking queries.  At the very least, you’d need to replace table with the actual table name.

一旦使用MySQL变得熟悉,您就会发现您将花费大部分时间来调整查询。 至少,您需要用实际的表名替换table

Now we need to execute this query on our established database connection:

现在,我们需要在已建立的数据库连接上执行此查询:

<? $mydata = mysql_query($query, $myDatabase) or die(mysql_error()); ?>

This retrieves our data from the MySQL database table, showing the cause of the error if the process cannot be completed. The information that is brought back from a SELECT is “raw data” – we need to convert it into a state we can use with PHP. The next line does just that, transforming the first row into an array, labelling each slot in the array with the name of the column the information relates to:

这将从MySQL数据库表中检索我们的数据,显示如果无法完成该过程的错误原因。 从SELECT返回的信息是“原始数据”-我们需要将其转换为可用于PHP的状态。 下一行就是这样做的,将第一行转换为一个数组,并用信息所涉及的列的名称标记该数组中的每个插槽:

<? $myrows = mysql_fetch_assoc($mydata); ?>

That’s essentially it. The entire code is:

本质上就是这样。 整个代码是:

<? $myDatabase = mysql_connect("mysql_server", "user", "password") or 
	trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db("database", $myDatabase);
$query = "SELECT * FROM table";
$mydata = mysql_query($query, $myDatabase) or die(mysql_error());
$myrows = mysql_fetch_assoc($mydata); ?>

So long as we know the names of the columns, we can print out the data from any column in the first row of retrieved data by using:

只要知道列名,就可以使用以下命令从检索到的数据的第一行中的任何列中打印出数据:

<?=$myrows['column_name’]?>

Note that this only works for the first row of data that is returned by our query (for example, details of just one person from a users database table). If we have more than one row of information that we wish to display, we will have to use some kind of loop to cycle through them… which will be discussed in the next entry for this series.

请注意,这仅适用于查询返回的第一行数据(例如,用户数据库表中一个人的详细信息)。 如果我们要显示的信息多于一行,我们将不得不使用某种循环来循环浏览这些信息……这将在本系列的下一篇文章中进行讨论。

重要的提示 (Important Note)

There are many changes we could make to this code, including compacting it into fewer lines, adding more security, and better error-checking. We could also completely change our approach to an object-oriented one by using mysqli.  The goal of what I have shown you is to establish a basic MySQL connection.

我们可以对该代码进行很多更改,包括将其压缩为更少的行,增加了安全性以及更好的错误检查。 我们也可以通过使用mysqli将我们的方法完全改变为面向对象的方法。 我向您展示的目标是建立一个基本的 MySQL连接。

翻译自: https://thenewcode.com/430/Write-Your-Own-MySQL-Connection-With-PHP

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值