MySQL Connect
Creating a PHP and MySQL Connection (创建PHP和MySQL连接)
Establishing a connection between PHP and MySQL is essential for building dynamic web applications. With this connection, you can access and manipulate data stored in a MySQL database through PHP scripts. This article provides a comprehensive guide to help you create a connection between PHP and MySQL. (在PHP和MySQL之间建立连接对于构建动态Web应用程序至关重要。通过此连接,您可以通过PHP脚本访问和操作存储在MySQL数据库中的数据。本文提供全面的指南,帮助您在PHP和MySQL之间创建连接。)
Prerequisites
Prerequisites (前提条件)
Before we dive into creating the connection, it’s important to make sure you have the following prerequisites in place:
A web server with PHP installed (such as Apache or Nginx) (-安装了PHP的Web服务器(如Apache或Nginx ))
A MySQL database (MySQL数据库)
PHP MySQL extension (included in most PHP installations) (- PHP MySQL扩展(包含在大多数PHP安装中))
Understanding PHP and MySQL Connection
Understanding PHP and MySQL Connection (了解PHP和MySQL连接)
A PHP and MySQL connection involves two main components: PHP and a MySQL database. PHP is a server-side scripting language used for creating dynamic web pages, while a MySQL database is used for storing and retrieving data.
To connect PHP and MySQL, you’ll need to use the mysqli (MySQL Improved) extension, which provides a set of functions for working with a MySQL database. With the mysqli extension, you can perform CRUD (Create, Read, Update, Delete) operations on a database through PHP scripts. (要连接PHP和MySQL ,您需要使用mysqli ( MySQL Improved )扩展,它提供了一组用于处理MySQL数据库的函数。使用mysqli扩展,您可以通过PHP脚本对数据库执行CRUD (创建、读取、更新、删除)操作。)
Establishing a Connection
Establishing a Connection (建立联系)
To establish a connection between PHP and MySQL, you’ll need to use the mysqli_connect() function. This function takes three parameters: the server name, username, and password.
Here is an example of how to use the mysqli_connect() function:
<?php
$server = "localhost";
$username = "your_username";
$password = "your_password";
// Establish connection
$conn = mysqli_connect($server, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
In the example above, $server is set to localhost, which is the default location of the MySQL server. $username and $password should be set to the username and password used to access the database. (在上面的示例中, $ server设置为localhost ,这是MySQL服务器的默认位置。$ username和$ password应设置为用于访问数据库的用户名和密码。)
After establishing the connection, we use the mysqli_connect_error() function to check if the connection was successful. If the connection fails, the script outputs an error message and terminates the connection. If the connection is successful, the script outputs “Connected successfully”. (建立连接后,我们使用mysqli_connect_error ()函数检查连接是否成功。如果连接失败,脚本将输出错误消息并终止连接。如果连接成功,脚本输出“已成功连接”。)
Selecting a Database
Selecting a Database (选择数据库)
Once you have established a connection to the MySQL server, you’ll need to select a database to work with. You can do this using the mysqli_select_db() function. (建立与MySQL服务器的连接后,您需要选择要使用的数据库。您可以使用mysqli_select_db ()函数执行此操作。)
Here is an example of how to use the mysqli_select_db() function:
<?php
$server = "localhost";
$username = "your_username";
$password = "your_password";
$db = "your_database";
// Establish connection
$conn = mysqli_connect($server, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Select database
$select_db = mysqli_select_db($conn, $db);
// Check database selection
if (!$select_db) {
die "Error selecting database: " . mysqli_error($conn));
}
echo "Database selected successfully";
?>
In the example above, $db
is set to the name of the database you want to select. After establishing the connection, we use the mysqli_select_db()
function to select the database. The function takes two parameters: the connection and the name of the database.
We then use the mysqli_error()
function to check if the database was selected successfully. If the selection fails, the script outputs an error message and terminates the connection. If the selection is successful, the script outputs “Database selected successfully”.
(然后,我们使用mysqli_error ()
函数来检查数据库是否已成功选择。如果选择失败,脚本将输出错误消息并终止连接。如果选择成功,脚本输出“数据库选择成功”。)
Closing the Connection
Closing the Connection (关闭连接)
When you are finished working with a MySQL database, it’s important to close the connection. This helps to free up resources and prevent potential security issues. (使用完MySQL数据库后,请务必关闭连接。这有助于释放资源并防止潜在的安全问题。)
To close the connection, you can use the mysqli_close() function. Here is an example of how to use the mysqli_close() function:
<?php
$server = "localhost";
$username = "your_username";
$password = "your_password";
$db = "your_database";
// Establish connection
$conn = mysqli_connect($server, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Select database
$select_db = mysqli_select_db($conn, $db);
// Check database selection
if (!$select_db) {
die("Error selecting database: " . mysqli_error($conn));
}
// Close connection
mysqli_close($conn);
echo "Connection closed";
?>
In the example above, we use the mysqli_close() function to close the connection. The function takes one parameter: the connection. The script then outputs “Connection closed” to confirm that the connection has been closed.
Conclusion
Conclusion (小结)
In this article, we have provided a comprehensive guide to help you create a connection between PHP and MySQL. We covered the prerequisites, understanding the connection, establishing a connection, selecting a database, and closing the connection. With this guide, you should now be able to create a connection between PHP and MySQL and perform CRUD operations on a database through PHP scripts. (在本文中,我们提供了一个全面的指南,以帮助您在PHP和MySQL之间建立连接。我们涵盖了先决条件、了解连接、建立连接、选择数据库和关闭连接。使用本指南,您现在应该能够在PHP和MySQL之间创建连接,并通过PHP脚本在数据库上执行CRUD操作。)