Introduction and use of PDO

1. PDO introduction

1.1 Link to the database

Method 1: mysql extension [php7 has been eliminated in this way]
Method 2: mysqli extension
Method 3: PDO extension

1.2 Introduction to PDO

The PDO (PHP Data Object) extension provides a lightweight and consistent interface for PHP to access various databases. No matter what database is accessed, it can be operated through a consistent interface.

1.2.1 Enable PDO extension

Open PDO connection MySQL extension

extension=php_pdo_mysql.dll

1.3 PDO core class

1. PDO class: indicates a connection between PHP and the database.
2. PDOStatement class.
First: indicates the relevant result set after executing the data query statement (select, show).
Second: preprocessing object
3. PDOException class: indicates PDO abnormal

1.4 instantiate PDO objects

grammar:

__construct($dsn, username, password)

1.4.1 DSN

DSN: data source name, the name of the data source, contains the information to connect to the database, the format is as follows:

$dsn=database type: host=host address; port=port number; dbname=database name; charset=charset

Database type:

MySQL database => mysql;
oracle database => oci;
SQL Server => sqlsrv; Please
refer to the manual "PDO Driver" for specific driver types

1.4.2 Instantiate PDO

The process of instantiating PDO is the process of connecting to the database.
Example:

<?php 
$dsn='mysql:host=localhost;port=3306;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

effect:
Insert picture description here

1.4.3 Matters needing attention

1. If you are connecting to a local database, host can be omitted.

<?php 
$dsn='mysql:port=3306;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

The effect is as above:
Insert picture description here
2. If the default port number is 3306, the port can also be omitted.

<?php 
$dsn='mysql:dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

The effect is as above:
Insert picture description here
3. Charset is also omitted. If omitted, the default character encoding is used.

<?php 
$dsn='mysql:dbname=data;';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

The effect is as above:
Insert picture description here
4. dbname can also be omitted, if it is omitted, no database is selected:
Example:

<?php 
$dsn='mysql:';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

Effect:
Insert picture description here
5. The host, port, dbname, and charset are not case sensitive and there is no order.
example:

<?php 
$dsn='mysql:port=3306;host=localhost;dbName=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');
var_dump($pdo);

effect:
Insert picture description here

Two, use PDO

2.1 Executing data manipulation statements

Method: $pdo->exec($sql)Execute data addition, deletion, and modification statements, and return the number of affected records if the execution is successful, and return false if the SQL statement is wrong.

2.1.1, increase

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

//  2、执行数据增加操作语句 
if ($pdo->exec("insert into shows values (null, 'kk', 'kkkkk', CURRENT_TIMESTAMP)"))
    echo '自动增长的编号是:'.$pdo->lastInsertId(),'<br>';

effect:
Insert picture description here

2.1.2, change

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行修改
echo $pdo->exec('update shows set title="bb" where id=2');

effect:
Insert picture description here
Insert picture description here

2.1.3, delete

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行删除
echo $pdo->exec('delete from shows where id=2');

effect:
Insert picture description here
Insert picture description here

2.1.4. Check:

2.1.4.1, return a two-dimensional array

1. Return associative and indexed arrays
<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.1 获取二维数组
// $rs = $stmt->fetchAll(PDO::FETCH_BOTH) // 默认返回关联和索引
$rs = $stmt->fetchAll(PDO::FETCH_BOTH); // 返回关联和索引数组
echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


2. Return the index array
<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.1 获取二维数组
// $rs = $stmt->fetchAll(PDO::FETCH_BOTH) // 默认返回关联和索引
$rs = $stmt->fetchAll(PDO::FETCH_NUM); // 返回索引数组
echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


3. Return associative array
<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.1 获取二维数组
// $rs = $stmt->fetchAll() // 默认返回关联和索引
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); // 返回关联数组数组
echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


4. Return an array of objects
<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.1 获取二维数组
// $rs = $stmt->fetchAll() // 默认返回关联和索引
$rs = $stmt->fetchAll(PDO::FETCH_OBJ); // 返回对象数组
echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


2.1.4.2, return a one-dimensional array

1. Associative and indexed arrays are returned by default

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.2 获取一维数组
$rs=$stmt->fetch(); // 默认返回关联和索引数组

echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


2. Take the index array

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.2 获取一维数组
$rs=$stmt->fetch(PDO::FETCH_NUM); // 默认返回索引数组,取一条,取完以后指针下移

echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


3. Get all data through loop

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
var_dump($stmt);
echo '<br>';
// 3、获取数据
// 3.2 获取一维数组
while($row=$stmt->fetch(PDO::FETCH_ASSOC)) {
    
    
    $rs[]=$row;
}

echo '<pre>';
var_dump($rs);

effect:
Insert picture description here


2.1.4.3, animal sequence

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
// 3、获取数据
// 3.3 匹配列
echo $stmt->fetchColumn(); // 默认获取当前行的第0列
// echo $stmt->fetchColumn(1); // 获取当前行第一列
echo '<pre>';

effect:
Insert picture description here


2.1.4.4, matching total number of rows, total number of columns

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');
// 3、获取数据
// 3.4 总行数、总列数
echo '总行数:'.$stmt->rowCount(),'<br>';
echo '总列数:'.$stmt->columnCount(),'<br>';

effect:
Insert picture description here


Three, traverse PDOStatement

Example:

<?php 
// 1、实例化PDO
$dsn='mysql:port=3306;host=localhost;dbname=data;charset=utf8';
$pdo=new PDO($dsn, 'root', 'root');

// 2、执行查询
$stmt = $pdo->query('select * from shows');

// PDOStatement对象中是有迭代器的
foreach($stmt as $row) {
    
    
    echo $row['id'],'-',$row['title'],$row['content'],'-',$row['createTime'];
    echo '<br>';
}

effect:
Insert picture description here


On the way of learning php, if you think this article is helpful to you, please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/112679703