Home > Backend Development > PHP Problem > Is there any difference between the ways php connects to mysql?

Is there any difference between the ways php connects to mysql?

WBOY
Release: 2023-03-15 20:30:01
Original
2710 people have browsed it

Difference: 1. PDO is used in 12 different databases, while MySQLi is only used in the mysql database; 2. The way PDO closes the connection is "$conn = null", while the way MySQLi closes the connection is "$conn->close()" or "mysqli_close()".

Is there any difference between the ways php connects to mysql?

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

Is there any difference between the ways in which php connects to mysql?

After php5.3 version, there are two ways to connect to the database, one is through mysqli, and the other is Connecting to the database through PDO and through mysqli can also be divided into two situations: mysqli (object-oriented), mysqli (process-oriented).
There are three ways:
1) PDO connects to mysql
2 ) mysqli (object-oriented) connects to the database
3) mysqli (process-oriented) connects to the database
(In fact, there is another connection method: using the MySQL extension. However, this extension is not recommended for use in 2012.)

PDO connection example

You can first check whether your php has PDO installed through the phpinfo() command (I use php7, it is already installed by default)
If it is not installed, refer to the web page: http://php.net/manual/en/pdo.installation.php
Is there any difference between the ways php connects to mysql?
Code example:

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

(Please pay attention to changes when using Database user name and password, as well as the selected database name (dbname)

mysqli (object-oriented) connection instance

You can first check yours through the phpinfo() command Whether mysqli has been installed in php (I use php7, it is already installed by default)
If not, please refer to the web page: http://php.net/manual/en/mysqli.installation.php
Is there any difference between the ways php connects to mysql?
Code example:

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

mysqli (process-oriented) connecting to the database

Code example:

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

The difference between the three :

1. How to close the connection:
PDO:

$conn = null;
Copy after login

MySQLi (object-oriented):

$conn->close();
Copy after login

MySQLi ( Process-oriented):

mysqli_close($conn);
Copy after login
  • PDO is applied in 12 different databases, MySQLi is only for MySQL database.
  • If your project needs to switch between multiple databases, it is recommended to use PDO , so you only need to modify the connection string and department query statement. Using MySQLi, if it is a different database, you need to rewrite all code, including queries.
  • Both are object-oriented, but MySQLi also provides API interface.
  • Both support prepared statements. Prepared statements can prevent SQL injection and are very important for the security of web projects.
    MySQLi The difference between object-oriented and process-oriented:
    Many PHP programmers are not used to object-oriented programming, so the mysqli class library provides this method for them to use. This is also convenient for some users who use the mysql extension to quickly migrate to mysqli. In fact, mysqli_query() is an encapsulation of the object-oriented calling process.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Is there any difference between the ways php connects to mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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