天天看点

示例数据库_PHP数据对象:访问和使用数据库–一个示例

示例数据库

示例数据库_PHP数据对象:访问和使用数据库–一个示例

To coincide with our tutorial on our forum on Accessing and Using Databases using PHP Data Objects, we’re going to example PHP Data Objects used to create a basic PHP application which outputs rows of a database table and adds new rows to the database through an HTML form.

为了与我们的论坛上使用PHP数据对象访问和使用数据库的教程相吻合,我们将以示例PHP数据对象为例,该示例用于创建一个基本PHP应用程序,该应用程序输出数据库表的行并通过一个新的行将新行添加到数据库中HTML表单。

示例数据库_PHP数据对象:访问和使用数据库–一个示例

使用PHP数据对象连接到数据库。 (Connecting to a database using PHP Data Objects.)

See the code

看代码

这里发生了什么事? (What’s happening here?)

  • “mysql:dbname=euktuts_jul52012;host=localhost” – this is the Data Source Name, for more information about the structural syntax of DSN and what each part means, see this on the PHP web site.

    “MySQL的:DBNAME = euktuts_jul52012;主机=本地主机” -这是数据源名称,关于DSN的语法结构以及各部分的手段,看到更多的信息, 这对PHP的网站。

  • try { $connection = new PDO($dsn, $dbuser, $dbuserpw); } catch (PDOException $e) { echo ‘There was a problem connecting to the database: ‘ . $e->getMessage(); } – this is a try..catch block, and will catch any errors that occur from the code within the try block. Essentially any errors returned by the PDO class are caught in the $e variable which is set to catch the error from the PDOException class.

    尝试{$ connection = new PDO($ dsn,$ dbuser,$ dbuserpw); } catch(PDOException $ e){echo'连接到数据库时出现问题:'。 $ e-> getMessage(); } –这是一个try..catch块,它将捕获try块中的代码中发生的所有错误。 基本上,PDO类返回的任何错误都包含在$ e变量中,该变量设置为捕获PDOException类中的错误。

  • require(“submissions.php”); – this contains the code that checks the submission (i.e. when adding a new row to the database). It is separate to keep code organised.

    require(“ submissions.php”); –这包含检查提交的代码(即,向数据库添加新行时)。 保持代码井井有条是分开的。

  • $query = $connection->query(“SELECT * FROM users”); – fetches the current users from the database.

    $ query = $ connection-> query(“ SELECT * FROM users”); –从数据库中获取当前用户。

  • foreach($query as $row) – the foreach goes through the values in a loop

    foreach($ query as $ row) – foreach循环遍历值

让我们看一下submissions.php 。 (Let’s take a look at submissions.php.)

See the code

看代码

这里发生了什么事? (What’s happening here?)

  • $username = $connection->quote($_POST[‘username’]); $password = $connection->quote($_POST[‘password’]); – this ensures the queries are safe from potential SQL injections because the start and end of the username and password strings are escaped after the postback has occurred, making it impossible for SQL injections to occur.

    $ username = $ connection-> quote($ _ POST ['username']); $ password = $ connection-> quote($ _ POST ['password']); –这可以确保查询不会受到潜在SQL注入的影响,因为回发发生后,用户名和密码字符串的开头和结尾将被转义,从而使SQL注入无法进行。

  • $add = $connection->exec(“INSERT INTO users (username, password) VALUES ($username,$password)”); the exec() method executes UPDATE and DELETE queries and returns the number of affected rows – does not work with SELECT queries as no rows would be affected. For SELECT queries, as shown above, you use the query() method.

    $ add = $ connection-> exec(“ INSERT INTO users(username,password)VALUES($ username,$ password)”); exec ()方法执行UPDATE和DELETE查询并返回受影响的行数– 不适用于SELECT查询,因为不会影响任何行 。 对于SELECT查询,如上所示,可以使用query ()方法。

  • if($add == 1) – if the number of affected rows is 1, theoretically it has been added to the table.

    if($ add == 1) –如果受影响的行数为1,理论上将其添加到表中。

That’s it. It’s that simple. For more examples, please see the thread on our Web Hosting Forum on Accessing and Using Databases using PHP Data Objects.

而已。 就这么简单。 有关更多示例,请参见Web托管论坛上有关使用PHP数据对象访问和使用数据库的主题 。

下载应用程序。 (Download the application.)

You can download the application to see the code in full. Please read the DISCLAIMER file as it governs your use of the application.

您可以下载该应用程序以查看完整的代码。 请阅读免责声明文件,因为它决定了您对应用程序的使用。

翻译自: https://www.eukhost.com/blog/webhosting/php-data-objects-accesing-and-using-databases-an-illustrative-example/

示例数据库