Home  >  Article  >  Backend Development  >  PHP study notes: database connection and operation

PHP study notes: database connection and operation

WBOY
WBOYOriginal
2023-10-09 11:42:141004browse

PHP study notes: database connection and operation

PHP study notes: Database connection and operation

Overview:
In Web development, database connection and operation are very important links. As a scripting language widely used in web development, PHP provides rich database connection and operation functions. This article will introduce how to connect to the database and common database operation methods in PHP, and will also provide specific code examples so that readers can better understand and apply them.

1. Database connection
In PHP, we can use extensions such as mysqli or PDO to connect to the database. The following is a code example for using the mysqli extension to connect to a MySQL database:

$servername = "localhost";
$username = "root";
$password = "password ";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check whether the connection is successful
if ($conn->connect_error) {

die("连接失败: " . $conn->connect_error);

}
echo "Connection successful";
?>

2. Database operation
After the connection is successful, we can perform various operations on the database, including query, insert, update, delete, etc. The following are some common database operation code examples:

  1. Query data
    Use the SELECT statement to query the data in the database and display the query results. The following code example demonstrates how to query all user records in a users table:

$sql = "SELECT id, username, email FROM users";
$ result = $conn->query($sql);

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
    echo "id: " . $row["id"]. " - Name: " . $row["username"]. " - Email: " . $row["email"]. "<br>";
}

} else {

echo "0 结果";

}
?>

  1. Insert data
    Use the INSERT statement to insert new data into the database. The following code example demonstrates how to insert a new record into the users table:

$sql = "INSERT INTO users (username, email) VALUES ('John', 'john@example.com')";
if ($conn->query($sql) === TRUE) {

echo "新记录插入成功";

} else {

echo "Error: " . $sql . "<br>" . $conn->error;

}
?>

  1. Update data
    Use the UPDATE statement to update data in the database. The following code example demonstrates how to update a user's email address:

$sql = "UPDATE users SET email='newemail@example.com' WHERE id=1";

if ($conn->query($sql) === TRUE) {

echo "记录更新成功";

} else {

echo "Error updating record: " . $conn->error;

}
?>

  1. Delete data
    Use the DELETE statement to delete data from the database. The following code example demonstrates how to delete a user record:

$sql = "DELETE FROM users WHERE id=1";

if ($conn ->query($sql) === TRUE) {

echo "记录删除成功";

} else {

echo "Error deleting record: " . $conn->error;

}
?>

3. Close the database connection
After completing the database operation, we need to close the database connection to release resources. The following is a code example for closing a database connection:

$conn->close();
?>

Conclusion:
This article is simple It introduces how to connect to the database in PHP and common database operation methods, and provides specific code examples. By learning and understanding this knowledge, readers can better apply PHP for database operations and improve the efficiency of Web development. Hope this article is helpful to readers.

The above is the detailed content of PHP study notes: database connection and operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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