PHP connection MySQL related knowledge and operations

jacklove
Release: 2023-03-25 13:58:01
Original
1733 people have browsed it

How to connect through phpmysql database, this chapter will explain the database connection in detail.

Connecting to MySQL

Before we access the MySQL database, we need to connect to the database server first:

Instance (MySQLi - Object-oriented)

<?php$servername = "localhost";$username = "username";$password = "password"; 
// 创建连接$conn = new mysqli($servername, $username, $password); 
// 检测连接if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);} echo "连接成功";?>
Copy after login

Note that $connect_error in the above object-oriented example was added in PHP 5.2.9 and 5.3.0. If you need to be compatible with earlier versions, please use the following code replacement:

// 检测连接
if (mysqli_connect_error()) {
    die("数据库连接失败: " . mysqli_connect_error());
}
Copy after login

Instance (MySQLi - process-oriented)

<?php$servername = "localhost";$username = "username";$password = "password"; 
// 创建连接$conn = mysqli_connect($servername, $username, $password); 
// 检测连接if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());}echo "连接成功";?>
Copy after login

Instance (PDO)

<?php$servername = "localhost";$username = "username";$password = "password"; 
try {
    $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);    echo "连接成功"; 
}catch(PDOException $e){
    echo $e->getMessage();}?>
Copy after login

Note that In the above PDO example, we have specified the database (myDB). PDO needs to set the database name during the connection process. If not specified, an exception will be thrown.

Close the connection

The connection will be automatically closed after the script is executed. You can also use the following code to close the connection:

Instance(MySQLi - Object Oriented)

$conn->close();
Copy after login

Instance(MySQLi - Procedural Oriented)

mysqli_close($conn);
Copy after login

Instance(PDO)

$conn = null;
Copy after login
This chapter provides a detailed understanding of the knowledge and operations of database connections. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

Introduction to PHP MySQL (database related knowledge)

PHP MySQL data reading operations and Method

How to use PHP to send email

The above is the detailed content of PHP connection MySQL related knowledge and operations. 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!