Blogger Information
Blog 23
fans 1
comment 0
visits 29623
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PDO操作
Liu
Original
5382 people have browsed it

一、PDO操作流程

1、连接数据库

//数据库连接参数$db = [    'type' => 'mysql',    'host' => 'localhost',    'dbname' => 'ouyangke',    'username' => 'root',    'password' => 'root'];

1.2、获取dsn参数

$dsn = $db['type'] . ':host=' . $db['host'] . ';dbname=' . $db['dbname'];

1.3、连接数据库

try {    $pdo = new PDO($dsn, $db['username'], $db['password']);//    print_r($pdo);} catch (PDOException $e) {    die('错误提示:'.$e->getMessage());};

1.4 prepare 预处理对象

$stmt = $pdo->prepare($sql);

1.5、pdo绑定参数

$name = 'els';$alias = '俄罗斯动画片';$stmt->bindParam('name', $name, PDO::PARAM_STR);$stmt->bindParam('alias', $alias, PDO::PARAM_STR);

1.5 PDOStatement类,对增删改查进行操作

//PDOStatement类,对增删改查进行操作$add = $stmt->execute();print_r($add);if ($add){    $count=$stmt->rowCount();    if ($count>0){        echo '数据插入成功';    }else{        echo '数据插入失败';    }}else{    die(print_r($stmt->errorInfo(),true));}

1.6 类连接,它是一直连接的,在执行完连接的代码后,自动释放

//unset($pdo);//print_r($pdo);$pdo=null;print_r($pdo);

手写代码




Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post