With the development of the Internet, the development of various websites and applications has become more and more common. In the development of these applications, the database is an essential component. This involves how to realize the connection between the database and php.
This article will introduce how to use php to connect to the database. Specifically, we will introduce the following content:
A database is a way to organize data. It can store various data, such as: user information, product information and order information. There are two main types of databases: relational and non-relational. What we introduce here is a relational database. The most popular of them is MySQL. MySQL is an open source database that is widely used for web application development.
Before php connects to the database, we need to create a database first. In MySQL we can create using phpMyAdmin or the console. We assume that a database named "test" has been created.
Step 1: Open the php file
First, we need to open a php file. In the file we can write php code to connect to the database.
Step 2: Establish a connection
To establish a connection to the database, we need to use PHP's built-in function mysqli_connect(). This function takes 4 parameters: hostname, username, password and database name.
$host = "localhost"; //Host name
$user = "root"; //User name
$password = ""; //Password
$database = "test"; //Database name
//Connect to database
$link = mysqli_connect($host, $user, $password, $database);
//Check the connection
if ($link === false) {
die("连接失败: " . mysqli_connect_error());
}
echo "Connection successful";
?>
In this example , we first define the host name, user name, password and database name, and then use the mysqli_connect() function to establish a connection to the database. If the connection fails, the program will display an error message. If the connection is successful, the program will display "Connection successful".
Step 3: Query data
After connecting to the database, we can use PHP's built-in function mysqli_query() to execute SQL queries. The query statement can be any valid MySQL query statement.
For example, we can query all data in the "users" table:
//Query data
$sql = "SELECT * FROM users";
$result = mysqli_query ($link, $sql);
//Check query results
if (mysqli_num_rows($result) > 0) {
//输出每一行数据 while ($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
"; }
} else {
echo "0 结果";
}
In this example, we use the mysqli_query() function to execute the query statement, and use the mysqli_num_rows() function to check whether the query result is empty. If the result is not empty, use the mysqli_fetch_assoc() function to iterate through the query results and output each data row.
mysqli is a new function introduced starting from PHP 5. It is an object-oriented API that can interact with MySQL database.
Let's rewrite our sample code above using mysqli functions:
$host = "localhost"; //Host name
$user = " root"; //Username
$password = ""; //Password
$database = "test"; //Database name
//Connect to database
$link = new mysqli($host, $user, $password, $database);
//Check the connection
if ($link->connect_error) {
die("连接失败: " . $link->connect_error);
}
echo "Connection successful";
//Query data
$sql = "SELECT * FROM users";
$result = $link->query($sql);
//Check query results
if ($result->num_rows > 0) {
//输出每一行数据 while ($row = $result->fetch_assoc()) { echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
"; }
} else {
echo "0 结果";
}
//Close the connection
$link->close();
?>
In this example, we use the mysqli function to establish a connection and connect to the database using the new object-oriented API. We then execute the query and iterate through the results to output rows of data. Finally, we close the connection.
PDO is an abstract database class that can interact with many types of databases. It is a new database connection method introduced in PHP 5.
Let’s rewrite our example code above using the PDO function:
$host = "localhost"; //Host name
$user = " root"; //Username
$password = ""; //Password
$database = "test"; //Database name
try {
//连接数据库 $pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password); // 设置PDO错误模式为异常 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "连接成功"; //查询数据 $sql = "SELECT * FROM users"; $stmt = $pdo->query($sql); //检查查询结果 if ($stmt->rowCount() > 0) { //输出每一行数据 while ($row = $stmt->fetch()) { echo "id: " . $row["id"] . " - Name: " . $row["name"] . " - Email: " . $row["email"] . "
"; } } else { echo "0 结果"; }} catch (PDOException $e) {
echo "连接失败: " . $e->getMessage();Copy after login
}
//Close the connection
$pdo = null;
?>
In this example, we Use the PDO function to establish a connection, use PDO to query the data, and if the result set exists, traverse each data row and output the data. Finally, we close the connection.
This article introduces how to use php to connect to the database. We started with an overview of databases and MySQL. Then, we introduced the steps for PHP to connect to the database, and used mysqli and PDO functions to demonstrate how to connect and query data.
Using php to connect to the database is a crucial part of developing web applications. Mastering this process will make development more efficient and faster.
The above is the detailed content of How to connect database and php to database. For more information, please follow other related articles on the PHP Chinese website!