实例
<?php /** * pdo新增数据 */ //链接数据库 $dsn = 'mysql:host=localhost;dbname=data'; $username = 'root'; $pwd = 'root'; $pdo = new PDO($dsn,$username,$pwd); //sql语句 $sql = "insert `stu` set `name`= :name,`address`= :address,`datetime`= :datetime"; //创建预处理对象 $stmt = $pdo->prepare($sql); //参数绑定 $data = ['name'=>'小龙女','address'=>'宋朝','datetime'=>time()]; //var_dump($data); //exit; $stmt->bindParam(':name',$data['name'],PDO::PARAM_STR); $stmt->bindParam(':address',$data['address'],PDO::PARAM_STR); $stmt->bindParam(':datetime',$data['datetime'],PDO::PARAM_INT); //var_dump($stmt->queryString); //exit; //执行sql语句 if($stmt->execute()) { echo '成功添加了'.$stmt->rowCount().'条记录'; }else{ echo '<h3>添加失败</h3>'; print_r($stmt->errorInfo()); exit(); } $stmt = null; $pdo = null;
运行实例 »
点击 "运行实例" 按钮查看在线实例