Correction status:qualified
Teacher's comments:
作业1:
作业2:
作业3:PDO连接数据库
<?php /** * Created by PhpStorm. * User: BIG-MAX * Date: 2018-08-31 * Time: 8:55 */ header('Content-type:text/html;charset=utf-8'); $dsn = 'mysql:host=127.0.0.1; dbname=oa_infomation'; $user = 'root'; $pass = 'root'; //实例化PDO类,创建pdo对象, try{ $stmt = new pdo($dsn, $user, $pass); //echo '<h2>连接成功!</h2>'; } catch (PDOException $e) { die('Connect ERROR! :' . $e->getMessage()); }
点击 "运行实例" 按钮查看在线实例
作业3:PDO新增记录操作
<?php header('Content-type:text/html;charset=utf-8'); /*新增数据*/ //连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=oa_infomation' , 'root' , 'root'); //$pdo = new pdo('mysql:host=127.0.0.1;port=3306;dbname=oa_infomation;charset=utf8','root','root',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,)); //准备SQL语句,占位符我们不再用?,用“命名占位符” //$sql = "INSERT 'use_unit' SET 'use_unit' = :use_unit , 'use_class' = :use_class"; $pdo->query("SET NAMES utf8"); $sql = "INSERT `use_unit` SET `use_unit`= :use_unit , `use_class`= :use_class"; //创建预处理对象 $stmt = $pdo->prepare($sql); $data = ['use_unit'=>'信息档案中心','use_class'=>'三级单位']; $stmt->bindParam(':use_unit',$data['use_unit'],PDO::PARAM_STR); $stmt->bindParam(':use_class',$data['use_class'],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; //echo $stmt->queryString;exit;
点击 "运行实例" 按钮查看在线实例