PHP database connection tutorial: beginner to database management master

WBOY
Release: 2024-06-01 22:27:59
Original
998 people have browsed it

To connect to the MySQL database, you need to create a database connection and use SQL queries to perform data operations (including retrieval, insertion, update, and deletion). Practical case demonstrates how to connect to a MySQL database and retrieve usernames. Specific steps include: creating a connection, executing the query, traversing the result set and printing the data.

PHP database connection tutorial: beginner to database management master

PHP Database Connection Tutorial: Beginner to Database Management Master

Prerequisite

  • PHP is installed on the local computer
  • Database management system (such as MySQL, PostgreSQL or MariaDB)

1. Create a database connection

<?php
// 数据库主机名
$hostname = 'localhost';
// 数据库用户名
$username = 'db_username';
// 数据库密码
$password = 'db_password';
// 数据库名称
$database = 'db_name';

// 连接到数据库
$conn = mysqli_connect($hostname, $username, $password, $database);
?>
Copy after login

2. Execute the SQL query

Once established Once connected, you can execute SQL queries.

<?php
// 查询字符串
$query = "SELECT * FROM table_name";

// 执行查询
$result = mysqli_query($conn, $query);

// 遍历结果集
while($row = mysqli_fetch_assoc($result)) {
    echo $row['column_name'];
}
?>
Copy after login

3. Insert data

<?php
// 插入数据
$query = "INSERT INTO table_name (column1, column2) VALUES ('value1', 'value2')";

// 执行查询
mysqli_query($conn, $query);
?>
Copy after login

4. Update data

<?php
// 更新数据
$query = "UPDATE table_name SET column1 = 'new_value' WHERE id = 1";

// 执行查询
mysqli_query($conn, $query);
?>
Copy after login

5. Delete data

<?php
// 删除数据
$query = "DELETE FROM table_name WHERE id = 1";

// 执行查询
mysqli_query($conn, $query);
?>
Copy after login

Practical case: Connect to MySQL database

Suppose you have a MySQL database named "users" and a table named "user_table" that contains usernames and passwords. Here's how to connect to that database and retrieve the username:

<?php
// 连接到 MySQL 数据库
$conn = mysqli_connect('localhost', 'root', '', 'users');

// 查询 user_table
$query = "SELECT username FROM user_table";

// 执行查询
$result = mysqli_query($conn, $query);

// 遍历结果集并打印用户名
while($row = mysqli_fetch_assoc($result)) {
    echo $row['username'] . PHP_EOL;
}
?>
Copy after login

The above is the detailed content of PHP database connection tutorial: beginner to database management master. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!