Table of Contents
初识PHP(四)PDO对象配置于使用,初识pdo
Home php教程 php手册 初识PHP(四)PDO对象配置于使用,初识pdo

初识PHP(四)PDO对象配置于使用,初识pdo

Jun 13, 2016 am 08:42 AM
sdk

初识PHP(四)PDO对象配置于使用,初识pdo

一、PDO的概念

  PDO其实就是一个数据库的抽象层,使用PDO编程可以方便的在之后的实际运营中随时更改数据库而不用变更源代码。PDO的位置如下图所示:

phptest","root","123");

  这就完成了PDO对象的初始化,所连接的数据库是mysql数据库的phptest这个数据库,使用的用户名和密码分别是root和123

  如果把dsn信息写到配置文件中,则使用如下方式:

  $pdo = new PDO("uri:MysqlDbo.ini","root","123");  \\dsn数据写在MysqlDbo.ini文件中

  

  3.2 PDO对象的使用

  PDO的成员方法如下:

  1 ) query($sql);      //用于执行查询SQL语句。返回PDOStatement对象     

  2 ) exec($sql);          //用于执行增、删、改操作,返回影响行数;     

  3 ) setAttribute();     //设置一个"数据库连接对象"属性。         

  4 ) fetchAll();        //解析数据

 

  下面举例:  

   数据库原始数据如下:

   1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //查询语句 10 $sql = 'select * from students'; 11 //执行语句、解析数据 12 echo '  id  '.'____________'.'name'.'____________'.'sex'."
"; 13 foreach ($pdo->query($sql) as $val){ 14 echo $val['id'].'____________'.$val['name'].'____________'.$val['sex']."
"; 15 } 16 ?>

 

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 $sql = "insert into students values('20125203','tony','female')"; 11 //执行语句、解析数据 12 $res = $pdo->exec($sql); 13 if ($res){ 14 echo "插入成功!"; 15 } 16 //查询结果 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 ?>

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "123"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 //$sql = "insert into students values('20125203','tony','female')"; 11 //修改语句 12 $sql = "update students set sex='male' where id='20125203'"; 13 //执行语句、解析数据 14 $res = $pdo->exec($sql); 15 if ($res){ 16 echo "修改成功!"; 17 } 18 //查询结果 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 ?> 修改数据

效果如图:

1 php 2 //连接数据库 3 try { 4 $pdo = new PDO("mysql:host=localhost;dbname=phptest", "root", "20125202"); 5 } 6 catch (PDOException $e){ 7 die("数据库连接失败".$e->getMessage()); 8 } 9 //插入语句 10 //$sql = "insert into students values('20125203','tony','female')"; 11 //修改语句 12 //$sql = "update students set sex='male' where id='20125203'"; 13 //删除语句 14 $sql = "delete from students where id='20125203'"; 15 //执行语句、解析数据 16 $res = $pdo->exec($sql); 17 if ($res){ 18 echo "删除成功!"; 19 } 20 //查询结果 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 ?> 删除数据

效果如图:

以上就是PDO的基本使用方法与增删查改等操作。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Microsoft is developing new blur effects for Windows 11 Microsoft is developing new blur effects for Windows 11 May 13, 2023 am 09:04 AM

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 Master the essential skills for secondary development of Java Hikvision SDK Sep 06, 2023 am 08:10 AM

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

what is sdk what is sdk Jan 06, 2023 pm 03:26 PM

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.

Windows App SDK 1.2 is now online, here's what's new Windows App SDK 1.2 is now online, here's what's new May 12, 2023 pm 06:07 PM

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 PHP SDK Installation and use of WeChat mini program PHP SDK Mar 27, 2024 am 09:33 AM

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.

What is the sdk folder in linux What is the sdk folder in linux Jul 11, 2023 pm 01:38 PM

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.

PHP implements open source Kafka SDK PHP implements open source Kafka SDK Jun 18, 2023 am 09:18 AM

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

Detailed explanation of how to install Alipay SDK in PHP? (Guide Sharing) Detailed explanation of how to install Alipay SDK in PHP? (Guide Sharing) Mar 22, 2023 am 11:24 AM

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.

See all articles