abstract:1、数据库2、新增数据操作<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.创建预处理对象STMT //添加数据 $sql =&
1、数据库
2、新增数据操作
<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.创建预处理对象STMT //添加数据 $sql = "INSERT INTO `user` (`name`,`sex`,`age`,`fenshu`,`create_time`) VALUES (:name,:sex,:age,:fenshu,:create_time)"; //3.验证SQL语句,创建预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $name = '张三'; $sex = 0; $age = 18; $fenshu = 85; $createTime = time(); $stmt->bindParam(':name',$name,PDO::PARAM_STR,20); $stmt->bindParam(':sex',$sex,PDO::PARAM_INT); $stmt->bindParam(':age',$age,PDO::PARAM_INT); $stmt->bindParam(':fenshu',$fenshu,PDO::PARAM_INT); $stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT); //5.执行添加 if($stmt->execute()){ echo($stmt->rowCount()>0) ? '成功添加了'.$stmt->rowCount().'条记录' : '没有记录被增加'; } else { exit(print_r($stmt->errorInfo(),true)); }
3、更新数据操作
<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.创建预处理对象STMT //更新数据 $sql = "UPDATE `user` SET `fenshu`=:fenshu,`create_time`=:create_time WHERE `id` = :id"; //3.验证SQL语句,创建预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $id = 2; $fenshu = 100; $createTime = time(); $stmt->bindParam(':id',$id,PDO::PARAM_STR,20); $stmt->bindParam(':fenshu',$fenshu,PDO::PARAM_INT); $stmt->bindParam(':create_time',$createTime,PDO::PARAM_INT); //5.执行更新 if($stmt->execute()){ echo($stmt->rowCount()>0) ? '成功更新了'.$stmt->rowCount().'条记录' : '没有记录被更新'; } else { exit(print_r($stmt->errorInfo(),true)); }
4、删除数据
<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.创建预处理对象STMT /* //添加数据 $sql = "INSERT INTO `user` (`name`,`sex`,`age`,`fenshu`,`create_time`) VALUES (:name,:sex,:age,:fenshu,:create_time)";*/ //更新数据 $sql = "DELETE FROM `user` WHERE `id` = :id"; //3.验证SQL语句,创建预处理对象 $stmt = $pdo->prepare($sql); //4.参数绑定 $id = 2; $stmt->bindParam(':id',$id,PDO::PARAM_STR,20); //5.执行删除 if($stmt->execute()){ echo($stmt->rowCount()>0) ? '成功删除了'.$stmt->rowCount().'条记录' : '没有记录被删除'; } else { exit(print_r($stmt->errorInfo(),true)); }
5、查询信息
<?php //1.创建PDO对象,连接数据库 $pdo = new PDO('mysql:host=127.0.0.1;dbname=chengjibiao','root','root'); //2.验证SQL语句,创建预处理对象 //查询指定字段和条件 //$sql = "SELECT `id`,`name`,`sex`,`age`,`fenshu`,`create_time` FROM `user` WHERE `fenshu`> 90"; //查询表中所有信息 $sql = "SELECT * FROM `user`"; $stmt = $pdo->prepare($sql); //3.执行 $stmt->execute(); //4.遍历结果 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ $rows[] = $row; } ?> <style> table,th,td{ border:1px solid #ccc; } table{ text-align:center ; border:1px solid #666; width:50%; margin:30px auto; border-collapse: collapse; } table caption{ font-size:18px; color:#333333; font-weight:bolder; margin-bottom: 15px; } table tr:first-child{ font-size:16px; color:white; background-color: deeppink; } </style> <table> <caption>成 绩 表</caption> <tr> <th>ID</th> <th>姓名</th> <th>性别</th> <th>年龄</th> <th>分数</th> <th>注册时间</th> </tr> <?php foreach ($rows as $row) : ?> <tr> <td><?php echo $row['id'] ?></td> <td><?php echo $row['name'] ?></td> <td><?php echo $row['sex'] ?></td> <td><?php echo $row['age'] ?></td> <td><?php echo $row['fenshu'] ?></td> <td><?php echo date('Y/m/d',$row['create_time']) ?></td> </tr> <?php endforeach; ?> </table>
PDO操作很重要,还有很多视频中没有提到的参数,要多练习
Correcting teacher:韦小宝Correction time:2019-02-11 09:50:10
Teacher's summary:嗯!不错不错!写的很棒!pdo创建数据表并且对表操作 写的都很好!继续加油吧!