1024programmer News PDO talk about php connection mysql

PDO talk about php connection mysql

1. First of all, let’s talk about what is pdo?

Encyclopedia says that
PDO extension defines a lightweight, consistent interface for PHP to access databases, which provides a data access abstraction layer, so that no matter what database is used, you can use consistent functions Execute queries and fetch data. PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0.

My personal understanding: PDO is an abstract class that provides us with an interface method for accessing data
2. How to configure pdo
1. My environment is: win7
Editor: sublime text2
Server: xampp
2. Find the installation file of xampp: find php.ini
3. Find extension_dir and: extension_dir=”C:/xampp/php/ext” extension_dir is equal to the path of the ext file
4. Find Dynamic Extensions and add the following after the wording similar to the following:
extension=php_pdo.dll
extension=php_pdo_mysql.dll

extension=pdo_mysql.so
If you already have the above lines of code, but there is a semicolon in front of it, just remove it. Because the database I use is mysql, only mysql is configured
5. First test whether the configuration is successful: create a new file under the docs of xampp

6. Get the following proof that the configuration is successful:

Three. Let’s start practicing:
Database Configuration:

encapsulates a query class:

Call method:

Enter in the browser: http://localhost/myworkplace/phpinfo.php to get the following results:


The following introduces some methods provided by the PDO class:
$dbh = new PDO($this->dsn, $this->username, $this->password); new an object
$dbh->query($sql); //Query
$dbh->beginTransaction();//Open transaction
$dbh->exec(“Insert INTO `test`.`table` (`name` ,`age`)VALUES (‘mick’, 22);”); //Add
$dbh->commit();//commit transaction
$dbh=null; //Disconnect

Here I only encapsulate a find method, we can encapsulate many custom query methods, and also customize the methods of adding, deleting, and modifying
Of course, this way is definitely not my own idea, it is based on other people’s code, to imitate.
In the future, I will work hard to study the code. At first I just thought about connecting to mysql through php, but this suddenly became a bottleneck for me to write code. I know that java connects to the database through jdbc. Then I started collecting,
Later, I discovered pdo and started to understand and digest it
So learning things requires thinking and practice, simple thinking is often not as much as hands-on learning

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/pdo-talk-about-php-connection-mysql/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索