Home  >  Article  >  Backend Development  >  How to use PHP MySQL for data connection?

How to use PHP MySQL for data connection?

慕斯
慕斯Original
2021-05-31 11:17:442389browse

The previous article introduced you to "Some "personal introduction" about my SQL! ! ! 》, this article continues to introduce to you how to use PHP MySQL for data connection? What methods or connections do you know? This article will lead you to understand how mysql is downloaded and how to connect data? Let’s take a look!

How to use PHP MySQL for data connection?

PHP MySQL connection

Let’s learn the PHP MySQL connection method together!

PHP 5 and above are recommended to use the following method to connect to MySQL:

  • MySQLi extension ("i" means improved)

  • PDO (PHP Data Objects)

Should I use MySQLi or PDO?

MySQLi and PDO have their own advantages:

PDO is used in 12 different databases, and MySQLi only targets MySQL databases.

So, if your project needs to switch between multiple databases, it is recommended to use PDO, so that you only need to modify the connection string and department query statement. With MySQLi, if you use a different database, you need to rewrite all code, including queries.

Both are object-oriented, but MySQLi also provides an API interface.

Both support prepared statements. Prepared statements can prevent SQL injection and are very important for the security of web projects.

MySQLi and PDO connect to MySQL instance

In this chapter and the following chapters, we will use the following three methods to demonstrate PHP operating MySQL:

  • MySQLi (object-oriented)

  • MySQLi (procedure-oriented)

  • PDO

MySQLi installation

Linux and Windows: In most cases, the MySQLi extension is automatically installed when the php5 mysql package is installed.

For installation details, please check: http://php.net/manual/en/mysqli.installation.php

You can check whether it is installed through phpinfo() Success:

How to use PHP MySQL for data connection?

PDO Installation

For installation details, please see: http://php.net/ manual/en/pdo.installation.php

You can check whether the installation is successful through phpinfo():

How to use PHP MySQL for data connection?

PHP connects to MySQL

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

(object-oriented) The specific code is as follows:

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

(process-oriented ) The specific code is as follows:

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

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();
}
?>

Close the connection

The connection will automatically closure. You can also use the following code to close the connection:

(Object-oriented) The specific code is as follows:

$conn->close();

(Process-oriented) The specific code is as follows:

mysqli_close($conn);

Example PDO:

$conn = null;

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to use PHP MySQL for data connection?. 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