天天看點

示例資料庫_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/

示例資料庫