Example sharing of using PDO to operate MySQL database (collection)

黄舟
Release: 2017-08-20 14:20:41
Original
1855 people have browsed it

The following editor will bring you a basic tutorial on operating MySQL with PDO (recommended). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look.

The PDO extension in PHP defines a lightweight, consistent interface for PHP to access the database. It provides a data access abstraction layer, so that no matter what is used Databases can perform queries and obtain data through consistent functions.

The PHP version supported by PDO is PHP5.1 and higher, and under PHP5.2 PDO is enabled by default.

The following is in php. PDO configuration in ini:


extension=php_pdo.dll
Copy after login

In order to enable support for a certain database, the corresponding extension needs to be opened in the php configuration file, for example To support MySQL, you need to enable the following extension


extension=php_pdo_mysql.dll
Copy after login

The following is the use of PDO to perform basic add, delete, modify and query operations on mysql

Create a test database, and then run the following SQL statement:


##

DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(10) NOT NULL DEFAULT '0', `user` char(20) DEFAULT NULL, PRIMARY KEY (`id`), KEY `idx_age` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Copy after login

Program code:


getMessage(); } //新增 $sql="insert into test (id,user) values (1,'phpthinking')"; $res=$pdo->exec($sql); echo '影响行数:'.$res; //修改 $sql="update test set user='phpthinking' where id=1"; $res=$pdo->exec($sql); echo '影响行数:'.$res; //查询 $sql="select * from test"; $res=$pdo->query($sql); foreach($res as $row){ echo $row['user'].'
'; } //删除 $sql="delete from test where id=1"; $res=$pdo->exec($sql); echo '影响行数:'.$res;
Copy after login

The above is the detailed content of Example sharing of using PDO to operate MySQL database (collection). 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
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!