<?php /** * PDO PHP数据对象,是PHP操作所有数据库的抽象层,提供了一个统一的访问接口 */ // 数据源:设置数据库的类型,以及数据库服务器和默认的数据库 $dsn = 'mysql:host=127.0.0.1; dbname=php'; //用户名 $user = 'root'; //密码 $pass = 'root'; //实例化PDO类,创建pdo对象,并完成了数据库的连接 try { $pdo = new PDO($dsn, $user, $pass); // echo '<h2>连接成功</h2>'; } catch (PDOException $e) { die('Connect ERROR! :'. $e->getMessage()); } // 关闭连接 //$pdo = null; //unset($pdo);
点击 "运行实例" 按钮查看在线实例
<?php /** * 新增记录 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "INSERT `user` SET `name`= :name , `email`= :email, `password`= sha1(:password)"; //创建预处理对象 $stmt = $pdo->prepare($sql); //绑定参数 $data = ['name'=>'杨过','email'=>'yg@php.cn','password'=>'123']; $stmt->bindParam(':name',$data['name'],PDO::PARAM_STR); $stmt->bindParam(':email',$data['email'],PDO::PARAM_STR); $stmt->bindParam(':password',$data['password'],PDO::PARAM_STR); if ($stmt->execute()) { // rowCount(): 返回受影响的记录数量 echo '<h3>成功添加了'.$stmt->rowCount().'条记录</h3>'; } else { echo '<h3>添加失败</h3>'; print_r($stmt->errorInfo()); exit(); } $stmt = null; // 关闭连接 $pdo = null;
点击 "运行实例" 按钮查看在线实例
<?php /** * 新增记录 */ //1连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //2创建预处理对象 $stmt = $pdo->prepare("INSERT `user` SET `name`= :name , `email`= :email, `password`= sha1(:password)"); //3.执行添加 $stmt->execute(['name'=>'金轮法王1','email'=>'yzp@php.cn','password'=>'123']); $stmt->execute(['name'=>'金轮法王2','email'=>'yzp@php.cn','password'=>'123']); $stmt->execute(['name'=>'金轮法王3','email'=>'yzp@php.cn','password'=>'123']); $stmt->execute(['name'=>'金轮法王4','email'=>'yzp@php.cn','password'=>'123']); $stmt->execute(['name'=>'金轮法王5','email'=>'yzp@php.cn','password'=>'123']); echo '<h3>成功添加了'.$stmt->rowCount().'条记录</h3>';
点击 "运行实例" 按钮查看在线实例
<?php /** * 更新记录 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "UPDATE `user` SET `email`= :email WHERE `id`= :id"; //创建预处理对象 $stmt = $pdo->prepare($sql); $stmt->execute(['email'=>'yzf@qq.com','id'=>5]); echo '<h3>成功更新了'.$stmt->rowCount().'条记录</h3>'; $stmt = null; // 关闭连接 $pdo = null;
点击 "运行实例" 按钮查看在线实例
<?php /** * 删除记录 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "DELETE FROM `user` WHERE `id`= :id"; //创建预处理对象 $stmt = $pdo->prepare($sql); if ($stmt->execute(['id'=>11])) { // rowCount(): 返回受影响的记录数量 echo '<h3>成功删除了'.$stmt->rowCount().'条记录</h3>'; } else { echo '<h3>无删除</h3>'; print_r($stmt->errorInfo()); exit(); } $stmt = null; // 关闭连接 $pdo = null;
点击 "运行实例" 按钮查看在线实例
<?php /** * PDO 预处理查询 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "SELECT `name`,`email` FROM `user` WHERE `id` < :id"; //创建预处理对象 $stmt = $pdo->prepare($sql); //执行查询 $stmt->execute(['id'=>5]); // 一次性取出 //$rows = $stmt->fetchAll(); //foreach ($rows as $row) { // echo 'name: ',$row['name'],', email: ',$row['email'],'<br>'; //} while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo var_export($row),'<br>'; }
点击 "运行实例" 按钮查看在线实例
<?php /** * PDO 预处理查询 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "SELECT `id`,`name`,`email` FROM `user` WHERE `id` < :id"; //创建预处理对象 $stmt = $pdo->prepare($sql); //执行查询 $stmt->execute(['id'=>5]); //获取一行一列,无法获取同一行其它列 //echo $stmt->fetchColumn(0),'<br>'; //指针自动下移,获取二行2列 //echo $stmt->fetchColumn(1),'<br>'; $stmt = $pdo->prepare("select * from staff where salary > :salary"); $stmt->execute(['salary'=>6000]); // 结果看上去是正确的,但是是有问题的,不要这样做 echo '工资大于6000的人数: '. $stmt->rowCount(); echo '<hr>'; $stmt = $pdo->prepare("select count(*) from staff where salary > :salary"); $stmt->execute(['salary'=>6000]); echo '工资大于6000的人数: '. $stmt->fetchColumn();
点击 "运行实例" 按钮查看在线实例
<?php /** * PDO 预处理查询 */ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); //准备SQL语句,占位符我们不再用?,用"命名占位符" $sql = "SELECT `name`,`email` FROM `user` WHERE `id` < :id"; //创建预处理对象 $stmt = $pdo->prepare($sql); //执行查询 $stmt->execute(['id'=>5]); //将结果集中的列绑定到变量上 $stmt->bindColumn('name', $name); $stmt->bindColumn('email', $email); //用列变量来遍历结果集 while ($stmt->fetch(PDO::FETCH_BOUND)) { echo '姓名: ',$name, ', 邮箱: ', $email, '<br>'; }
点击 "运行实例" 按钮查看在线实例