


First introduction to PHP (4) PDO object configuration and use, first introduction to pdo_PHP tutorial
First introduction to PHP (4) PDO object configuration and use, first introduction to pdo
1. The concept of PDO
PDO is actually an abstraction layer of a database. Using PDO programming can easily change the database at any time in subsequent actual operations without changing the source code. The location of PDO is as shown below:
phptest","root","123");
This completes the initialization of the PDO object. The connected database is the phptest database of the mysql database. The username and password used are root and 123 respectively
If you write dsn information into the configuration file, use the following method:
$pdo = new PDO("uri:MysqlDbo.ini","root","123"); \dsn data is written in the MysqlDbo.ini file
3.2 Use of PDO objects
The member methods of PDO are as follows:
1) query($sql); //Used to execute query SQL statements. Return PDOStatement object
2) exec($sql); //Used to perform add, delete, and modify operations and return the number of affected rows;
3) setAttribute(); //Set a "database connection object" attribute.
4) fetchAll(); //Parse data
The following is an example:
The original data of the database is as follows:
1 php
2 //Connect to database
3 try {
4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202" );
5 }
6 catch (PDOException $e){
7 die("Database connection failed".$e->getMessage());
8 }
9 //Query statement
10 $sql = 'select * from students';
11 //Execute statements and parse data
12 echo 'id '.'____________'.'name'.'____________'.'sex'."
";
13 foreach ($pdo->query($sql) as $val){
14 echo $val['id'].'____________'.$val['name'].' ____________'.$val['sex']."
";
15 }
16 ?>
The effect is as shown below:
1 php
2 //Connect to database
3 try {
4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123" );
5 }
6 catch (PDOException $e){
7 die("Database connection failed".$e->getMessage());
8 }
9 //Insert statement
10 $sql = "insert into students values('20125203','tony','female')";
11 //Execute statements and parse data
12 $res = $pdo->exec($sql);
13 if ($res){
14 echo "Insertion successful!";
15 }
16 //Query results
17 $sql = 'select * from students';
18 echo 'id '.'____________'.'name'.'____________'.'sex'."
";
19 foreach ($pdo->query($sql) as $val){
20 echo $val['id'].'____________'.$val['name'].' ____________'.$val['sex']."
";
21 }
22 ?>
The effect is as shown below:
1 php
2 //Connect to database
3 try {
4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123" );
5 }
6 catch (PDOException $e){
7 die("Database connection failed".$e->getMessage());
8 }
9 //Insert statement
10 //$sql = "insert into students values('20125203','tony','female')";
11 //Modify the statement
12 $sql = "update students set sex='male' where id='20125203'";
13 //Execute statements and parse data
14 $res = $pdo->exec($sql);
15 if ($res){
16 echo "Modification successful!";
17 }
18 //Query results
19 $sql = 'select * from students';
20 echo ' id '.'____________'.'name'.'____________'.'sex'."
";
21 foreach ($pdo->query($sql) as $val){
22 echo $val['id'].'____________'.$val['name'].' ____________'.$val['sex']."
";
23 }
24 ?>
Modify data
The effect is as shown below:
1 php
2 //Connect to database
3 try {
4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202" );
5 }
6 catch (PDOException $e){
7 die("Database connection failed".$e->getMessage());
8 }
9 //Insert statement
10 //$sql = "insert into students values('20125203','tony','female')";
11 //Modify the statement
12 //$sql = "update students set sex='male' where id='20125203'";
13 //Delete statement
14 $sql = "delete from students where id='20125203'";
15 //Execute statements and parse data
16 $res = $pdo->exec($sql);
17 if ($res){
18 echo "Delete successfully!";
19 }
20 //Query results
21 $sql = 'select * from students';
22 echo 'id '.'____________'.'name'.'____________'.'sex'."
";
23 foreach ($pdo->query($sql) as $val){
24 echo $val['id'].'____________'.$val['name'].' ____________'.$val['sex']."
";
25 }
26 ?>
Delete data
The effect is as shown below:
The above is the basic usage of PDO and operations such as adding, deleting, checking and modifying.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











The new Windows 11 SDK for build 22523 revealed that Microsoft is developing a new blur effect for Windows 11. This effect is called Tabbed, and is in addition to Acrylic and Mica. The new DWMWA_SYSTEMBACKDROP_TYPE in the 22523 SDK, Mica, Acrylic’s public Win32 API and their weird new “tab” mix: pic.twitter.com/dbsu7ZFiIi — It’s All Back (@StartIsBack) December 15, 2021 Available in the following SDK’s Sample application

Master the essential skills for secondary development of Java Hikvision SDK Introduction: With the rapid development of information technology, video surveillance systems have been widely used in various fields. As the leading domestic video surveillance solution provider, Hikvision’s products and technologies have always occupied an important position in the market. In order to meet the needs of different projects, Hikvision provides SDK for developers to carry out secondary development. This article will introduce some essential skills for mastering the secondary development of Java Hikvision SDK, and attach corresponding code examples. 1. Understand Hikvision

The full name of sdk is "Software Development Kit", which means "software development kit" in Chinese. It is a set of tools provided by manufacturers of hardware platforms, operating systems (OS) or programming languages. SDKs assist software developers in creating applications for specific platforms, systems or programming languages. A basic SDK usually consists of a compiler, debugger, and application programming interface (API), but may also include other content, such as: documentation, libraries, runtime/development environment, testing/analysis tools, network protocols, etc.

The WindowsAppSDK is a set of tools and APIs that developers can use in their Windows applications to provide "consistent" functionality across a variety of devices using Windows 10 (version 1809 and later) and Windows 11. It's really important to understand that it doesn't replace existing application types like .NET or Windows SDK, it just provides a unified API toolset that can be used to complement your existing applications. Today, Microsoft released version 1.2 of Windows App SDK with many new features. The highlight of this release may be third-party developers

Installation and use of WeChat Mini Program PHPSDK With the rapid development of the mobile Internet, WeChat Mini Program has become a new way for more and more companies to conduct business and promote products. WeChat Mini Program PHPSDK provides developers with convenient and fast development tools, which can greatly improve development efficiency. This article will introduce the installation and use of WeChat applet PHPSDK. 1. Install SDK 1. Download the project file on GitHub. WeChat applet PHPSDK is an open source project. Developers can download it on GitHub.

This article will provide a detailed installation guide on the PHP Alipay SDK to help developers complete the installation of the SDK in a few simple steps.

The SDK in Linux is a folder that contains tools and resources such as compilers, debuggers, library files, header files, etc. SDK is the abbreviation of "software development kit", which means software development kit. It is an integrated environment provided for developers to develop and build applications, especially those running on the Linux operating system.

With the rapid development of the Internet, a large amount of data needs to be transmitted and processed. Therefore, the messaging system, as one of the classic applications of data transmission and processing, has become an indispensable part of the Internet architecture. Kafka is widely used in enterprise data architecture as a high-performance, distributed, scalable messaging system that supports real-time data processing. When using Kafka, an important issue is how to call Kafka's API. The development team provides open source clients in multiple languages for this purpose, and the open source KafkaS implemented in PHP
