我懒蛋又回来了!-PDO,懒蛋!-PDO
我懒蛋又回来了!-PDO,懒蛋!-PDO
hi
好几天了吧,脚伤都有一周了的。玩乐的这么久才发觉,对于年轻人,或者更具体的,对我而言,受伤最难受的不是受伤瞬间的身痛,不是随之而来的心理负担,不是独自一人远在他乡的孤独无助之感;最伤的是斗志,是受伤后提不起来的消沉意志,是破罐子破摔的糜烂心气,或者,是问题本质的内心莫名的恐惧。
还好,虽然这么多天没动,恐惧的双刃剑还是刺痛尚存的责任心、内疚感,我还能继续写博客,活活活活~~
争取一天一门课,回家之前能有所成,一起加油!
新人新气象新课程~
1、PDO
一、PDO简介
1.1 PDO
PHP Data Object,数据库访问抽象层,统一各种数据库的访问接口。
提高数据库之间的移植性以及可维护性——手段是抽象度的提高,访问接口的统一。
不管用什么数据库,可以用同一个API进行编写。
--特性
编码一致性;
灵活性;
高性能;(c语言编写的,编译为PHP)
OOP。
--支持的库
PDO只是一个抽象的接口,对该接口的操作还需要支持各种库。
这里我们用的是PHP语言,所以用PDO MySQL。
1.2 配置与启用
在PHP的配置文件进行配置:
开启php_pdo.dll拓展-》开启相对应的数据库的拓展php_pdo_mysql.dll-》phpinfo看一下。
当然不同的环境(集成)是不一样的,自行百度吧。
1.3 连接数据库
--方式
参数形式;
URI形式;
配置文件形式。php.ini
--栗子
/*
* PDO实现连接数据库
*/
//参数形式
try {
$dsn='mysql:localhost;dbname=imooc_pdo';//数据源
$username='root';
$passwd='';
$pdo=new PDO($dsn, $username, $passwd); //pdo对象
var_dump($pdo);
}catch (PDOException $e){ //得到错误信息
echo $e->getMessage();
}
//URI形式
try {
$dsn='uri:C:\wamp\www\PDO_learning\dsn.txt';//区别就在于数据源获取
$username='root';
$passwd='';
$pdo=new PDO($dsn, $username, $passwd);
var_dump($pdo);
}catch (PDOException $e){ //得到错误信息
echo $e->getMessage();
}
//配置文件形式,首先要在php.ini中写pdo.dsn.imooc="mysql:host=localhost;dbname=imooc_pdo"
try {
$dsn='imooc';//区别就在于数据源获取
$username='root';
$passwd='';
$pdo=new PDO($dsn, $username, $passwd);
var_dump($pdo);
}catch (PDOException $e){ //得到错误信息
echo $e->getMessage();
}
基本流程就是写好参数,然后new一个PDO对象即可。参数有数据源信息,用户名,密码三个。
数据源:data source name:驱动器名称:主机;数据库;(各种数据库这里的语法是不同的,自查)。
可以看到,三种形式的区别就在于取数据源的方式不同。
建议通过参数形式连接,当然,随你。
二、PDO对象的使用(增删改查)
2.1 exec()
执行一条SQL语句,并返回其受影响的行数,如果没有受影响的记录返回为0。
注意,exec对select没有用。
--栗子:建表&增
/*
* exec()的使用案例
*/
$pdo=new PDO('mysql:host=localhost;dbname=imooc','root','');
$sql= CREATE TABLE IF NOT EXISTS user(
id INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
password CHAR(32) NOT NULL,
email VARCHAR(30) NOT NULL
);
EOF;
$pdo->exec('use imooc_pdo');
$result0=$pdo->exec($sql);
var_dump($result0);
$insert='insert user(username,password,email) values("Kinga","'.md5('king').'","shit@shit.com")';
$result1=$pdo->exec($insert);
var_dump($result1);
但是,如果重复执行的话,会返回false。(?我也不知道为什么,谁能告诉我)
同样的删除也是可以的,但是注意查select是不能用exec执行的。
每次从exec的返回结果进行结果的判定就行。
---------------------------------------------------------------------
哎呀,我真欠打,看了两集朝廷的综艺节目,时间哗哗的。。明天加油吧。

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











PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

As a popular programming language, PHP is widely used in the field of web development. Among them, PHP's PDO_PGSQL extension is a commonly used PHP extension. It provides an interactive interface with the PostgreSQL database and can realize data transmission and interaction between PHP and PostgreSQL. This article will introduce in detail how to use PHP's PDO_PGSQL extension. 1. What is the PDO_PGSQL extension? PDO_PGSQL is an extension library of PHP, which

PHP and PDO: How to perform batch inserts and updates Introduction: When using PHP to write database-related applications, you often encounter situations where you need to batch insert and update data. The traditional approach is to use loops to perform multiple database operations, but this method is inefficient. PHP's PDO (PHPDataObject) provides a more efficient way to perform batch insert and update operations. This article will introduce how to use PDO to implement batch insert and update operations. 1. Introduction to PDO: PDO is PH

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

PHP and PDO: How to perform a full-text search in a database In modern web applications, the database is a very important component. Full-text search is a very useful feature when we need to search for specific information from large amounts of data. PHP and PDO (PHPDataObjects) provide a simple yet powerful way to perform full-text searches in databases. This article will introduce how to use PHP and PDO to implement full-text search, and provide some sample code to demonstrate the process. first

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

PHP and PDO: How to handle JSON data in databases In modern web development, processing and storing large amounts of data is a very important task. With the popularity of mobile applications and cloud computing, more and more data are stored in databases in JSON (JavaScript Object Notation) format. As a commonly used server-side language, PHP's PDO (PHPDataObject) extension provides a convenient way to process and operate databases. Book

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq
