Detailed explanation of how PDO operates MySQL in PHP
This article mainly introduces the method of operating MySQL with PHP PDO. It analyzes in detail the operating techniques related to opening PDO and database creation, connection, addition, deletion, modification and query in PHP based on examples. Friends in need can refer to the following
The details are as follows:
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 database is used, it can be accessed through Consistent functions execute queries and retrieve data.
The PHP version supported by PDO is PHP5.1 and higher, and PDO is enabled by default under PHP5.2.
The following is the configuration of PDO in php.ini:
extension=php_pdo.dll
In order to enable support for a certain database, you need to open the corresponding extension in the php configuration file. For example, to support MySQL, you need to open the following extension
extension=php_pdo_mysql.dll
The following is to use PDO to perform basic add, delete, modify and query operations on mysql
Create the 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;
Program code:
<?php header("content-type:text/html;charset=utf-8"); $dsn="mysql:dbname=test;host=localhost"; $db_user='root'; $db_pass='admin123'; try{ $pdo=new PDO($dsn,$db_user,$db_pass); }catch(PDOException $e){ echo '数据库连接失败'.$e->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'].'<br/>'; } //删除 $sql="delete from test where id=1"; $res=$pdo->exec($sql); echo '影响行数:'.$res;
Related recommendations:
Instance analysis of PDO database operation class encapsulated by PHP
PHP method of implementing SQLite operation class based on PDO
php inpdoPublic class definition and usage
The above is the detailed content of Detailed explanation of how PDO operates MySQL in PHP. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

The page is blank after PHP connects to MySQL, and the reason why die() function fails. When learning the connection between PHP and MySQL database, you often encounter some confusing things...

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content
