In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are the differences between the ways in which php connects mysql". In daily operation, I believe that many people have doubts about the differences between the ways in which php connects mysql. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what are the differences between the ways in which php connects mysql?" Next, please follow the editor to study!
Differences: 1, PDO is used in 12 different databases, while MySQLi is only used in mysql databases; 2, PDO closes connections by "$conn = null", while MySQLi closes connections by "$conn- > close ()" or "mysqli_close ()".
This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.
Is there any difference in the way php connects to mysql?
After the php5.3 version, there are two ways to connect to the database, one is through mysqli, the other is through PDO, and connecting to the database through mysqli can also be divided into two cases: mysqli (object-oriented) and mysqli (process-oriented).
There are three ways:
1) PDO connection mysql
2) mysqli (object oriented) to connect to the database
3) mysqli (process oriented) to connect to the database
(there is actually another way to connect: using MySQL extensions. However, this extension is not recommended starting in 2012.)
PDO connection instance
You can first use the phpinfo () command to check whether your php has installed PDO (I use php7, which is already installed by default)
If it is not installed, refer to the web page: http://php.net/manual/en/pdo.installation.php
Code example:
(please pay attention to changing the database user name and password, as well as the selected database name (dbname)
Mysqli (object-oriented) connection instance
You can first use the phpinfo () command to check whether your php has installed mysqli (I use php7, which is already installed by default)
If it is not installed, refer to the web page: http://php.net/manual/en/mysqli.installation.php
Code example:
Mysqli (process-oriented) connection to database
Code example:
The difference between the three:
1. How to close the connection:
PDO:
$conn = null
MySQLi (object oriented):
$conn- > close ()
MySQLi (process oriented):
Mysqli_close ($conn)
PDO is used in 12 different databases, and MySQLi is only for MySQL databases.
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 statements. With MySQLi, if you have different databases, you need to rewrite all the code, including queries.
Both are object-oriented, but MySQLi also provides an API interface.
Both support preprocessing statements. Preprocessing statements prevent SQL injection, which is very important for the security of web projects.
The difference between MySQLi object-oriented and process-oriented:
Multi-PHP programmers are not used to object-oriented bai programming, so the mysqli class library provides du as a method for them to use. It is also convenient for users who extend zhi with mysql to quickly migrate to mysqli. In fact, the inside of mysqli_query () is the encapsulation of the object-oriented calling procedure.
At this point, the study on "what are the differences between the ways in which php connects to mysql" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 227
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.