Correction status:Uncorrected
Teacher's comments:
pdo_connect.php
<?php $dsn = 'mysql:host=127.0.0.1;dbname=php;charset=utf8;port=3306'; $user = 'root'; $pass = 'root'; $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, //错误模式 PDO::ATTR_CASE => PDO::CASE_NATURAL, // 自然名称 PDO::ATTR_EMULATE_PREPARES => true, // 启用模拟功能 PDO::ATTR_PERSISTENT => true, ]; try { $pdo = new PDO($dsn, $user, $pass, $options); // echo 'connect ok!'; } catch (PDOException $e) { print '连接错误' . $e->getMessage(); die(); } // unset($pdo) // $pdo = null
点击 "运行实例" 按钮查看在线实例
pdo_pre_insert.php
<?php // 创建pdo对象 $pdo = new PDO('mysql:dbname=php', 'root', 'root'); // 准备sql语句 $sql = "insert `user` set `user_name` = :user_name, `email` = :email, `password` = sha1(:password)"; // 创建预处理对象 $stmt = $pdo->prepare($sql); // 绑定参数 $info = ['user_name'=>'张三丰', 'email'=>'zsf@zsf.com', 'password'=>'123']; $stmt->bindParam(':user_name',$info['user_name'],PDO::PARAM_STR); $stmt->bindParam(':email',$info['email'],PDO::PARAM_STR); $stmt->bindParam(':password',$info['password'],PDO::PARAM_STR); if ($stmt->execute()) { echo '添加了' . $stmt->rowCount() . '条记录'; } else { echo '添加失败'; print_r($stmt->errorInfo()); die(); }
点击 "运行实例" 按钮查看在线实例
pdo_pre_delete.php
<?php $pdo = new PDO('mysql:dbname=php', 'root', 'root'); $sql = "delete from user where user_id = :id"; $stmt = $pdo->prepare($sql); $id = 4; // $data = ['user_id'=> 4] $stmt->bindParam(':id',$id,PDO::PARAM_INT); //if ($stmt->execute($data)){ if ($stmt->execute()){ echo '成功的删除了' . $stmt->rowCount() . '数据'; }else { echo '删除失败'; print_r($stmt->errorInfo()); }
点击 "运行实例" 按钮查看在线实例
pdo_pre_update.php
<?php $pdo = new PDO('mysql:dbname=php', 'root', 'root'); $pdo->prepare("update user set user_name = :user_name, email = :email where user_id = :user_id")->execute(['user_name'=>'张老道','email'=>'oye@oye.com','user_id'=>5]);
点击 "运行实例" 按钮查看在线实例
pdo_pre_query.php
<?php //创建pdo对象 $pdo = new PDO('mysql:dbname=php', 'root', 'root'); $sql = "select * from staff where age > :age"; //创建预处理语句 $stmt = $pdo->prepare($sql); // 绑定参数 $age = 30; $stmt->bindParam(':age', $age, PDO::PARAM_INT); // bindParam第二个参数只能是变量 //$stmt->bindValue(':age', 22, PDO::PARAM_INT) // bindValue 第二个参数可以是字面量 // 执行查询 $stmt->execute(); // 得到结果集 //$stmt->setFetchMode(PDO::FETCH_ASSOC); //while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // echo '<pre>'; // print_r($row); // echo '</pre>'; //} $rows = $stmt->fetchAll(); echo '<pre>'; print_r($rows); echo '</pre>';
点击 "运行实例" 按钮查看在线实例
pdo_pre_query2.php
<?php $pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root'); $stmt = $pdo->prepare("select * from staff where age > :f_age"); $stmt->execute(['f_age' => 30]); // 将结果集中的列与变量进行绑定 $stmt->bindColumn('name', $name); $stmt->bindColumn('age', $age); while ($stmt->fetch()) { echo '姓名为: ' . $name . ' 年纪为: ' . $age; echo '<hr>'; } //$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
点击 "运行实例" 按钮查看在线实例
pdo_pre_query3.php
<?php /* * 预处理多语句查询操作 */ // 创建pdo对象 $pdo = new PDO('mysql:host=127.0.0.1;dbname=php', 'root', 'root'); // 准备sql语句 $sql1 = "select * from staff where salary < :salary"; $sql2 = "select * from staff where age > :age"; $stmt1 = $pdo->prepare($sql1); $stmt2 = $pdo->prepare($sql2); // 查找工资小于5000的员工 $stmt1->execute(['salary' => 5000]); echo '<pre>'; var_export($stmt1->fetchAll(PDO::FETCH_ASSOC)); echo '</pre>'; //关闭第一个预处理对象 $stmt1->closeCursor(); echo '<hr>'; // 查找年纪大于50的员工 $stmt2->execute(['age' => 50]); echo '<pre>'; var_export($stmt2->fetchAll(PDO::FETCH_ASSOC)); echo '</pre>';
点击 "运行实例" 按钮查看在线实例