Home > Backend Development > PHP Tutorial > PHP database connection from entry to practical application: master it step by step

PHP database connection from entry to practical application: master it step by step

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2024-05-31 10:32:04
Original
794 people have browsed it

Database connection in PHP consists of two phases: creating a connection (using MySQLi or PDO to establish communication with the database server) and executing the query (preparing, binding parameters and executing query statements). In the user registration exercise, bind the user data to the query statement and execute it to insert the new user into the database.

PHP database connection from entry to practical application: master it step by step

PHP database connection: from entry to practical application

1. Introductory knowledge

1.1 Basic concepts

Database The connection is the bridge that establishes communication between the PHP code and the database server.

1.2 Database driver

PHP provides a variety of database drivers, the most commonly used of which are MySQLi and PDO.

2. Connect to the database

2.1 Use MySQLi

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

// 创建 MySQLi 连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 使用连接
// ...
Copy after login

2.2 Use PDO

$dsn = "mysql:host=localhost;dbname=database_name";
$username = "username";
$password = "password";

// 创建 PDO 连接
$conn = new PDO($dsn, $username, $password);

// 检查连接是否成功
if (!$conn) {
    die("连接失败");
}

// 使用连接
// ...
Copy after login

3. Execute query

3.1 MySQLi

// 准备查询语句
$query = $conn->prepare("SELECT * FROM users");

// 执行查询
$query->execute();

// 获取结果
$result = $query->get_result();

// 遍历结果
while ($row = $result->fetch_assoc()) {
    echo $row["name"] . "<br>";
}
Copy after login

3.2 PDO

// 准备查询语句
$stmt = $conn->prepare("SELECT * FROM users");

// 执行查询
$stmt->execute();

// 获取结果
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);

// 遍历结果
foreach ($result as $row) {
    echo $row["name"] . "<br>";
}
Copy after login

4. Practical case: User registration

4.1 Connect to the database and prepare the query

// ... 同上连接数据库部分

// 准备查询语句
$stmt = $conn->prepare("INSERT INTO users (name, email, password) VALUES (?, ?, ?)");
Copy after login

4.2 Bind parameters and execute the query

// 绑定参数
$name = "John Doe";
$email = "john.doe@example.com";
$password = "securepassword";
$stmt->bind_param("sss", $name, $email, $password);

// 执行查询
$stmt->execute();
Copy after login

5. Note

  • Always check whether the connection is successful.
  • Always use prepared statements to prevent SQL injection attacks.
  • Properly release connection resources.

The above is the detailed content of PHP database connection from entry to practical application: master it step by step. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template