abstract:<?php /** * Created by PhpStorm. * User: 普通用户 * Date: 2019/5/6 * Time: 20:06 */ include "config.php"; $con
<?php /** * Created by PhpStorm. * User: 普通用户 * Date: 2019/5/6 * Time: 20:06 */ include "config.php"; $conn=connect($db); if($conn){ echo "数据库连接成功!","<br>"; } //删除数据 $sql="DELETE FROM `pl_pinglun` WHERE pl_id=2;"; $ret=delete($conn,$sql); if($ret){ echo "成功删除".$ret."条数据"; }else{ echo "删除失败"; } //插入数据 $sql = "INSERT INTO `pl_pinglun`(pl_user,pl_content,pl_appraise,pl_time) VALUES('用户2','一般般','中评','1557148842')"; $ret=insert($conn,$sql); if($ret){ echo "插入第".$ret."条计录"; }else{ echo "插入数据失败"; } //修改数据 $sql="UPDATE `pl_pinglun` SET pl_user='用户4',pl_content='太好了,下次再来买',pl_appraise='好评',pl_time='1557148845' WHERE pl_id=15 ;"; $ret=mod($conn,$sql); if($ret){ echo "修改成功"; }else{ echo "修改失败"; } //查询数据 $time=time(); $ret=select($conn,'pl_pinglun','pl_user="用户3"','pl_id DESC'); if($ret){ print_r($ret); }else{ echo "查询失败"; }
<?php /** * Created by PhpStorm. * function.php 方法库 * Date: 2019/5/6 * Time: 21:24 */ //连接数据库 function connect($db){ $conn=@mysqli_connect($db['host'],$db['user'],$db['pass'],$db['name'],'3306'); if(!$conn){ exit('数据库报错:'.mysqli_connect_error()); } return $conn; } //删除数据库 function delete($db,$sql){ $ret=mysqli_query($db,$sql); return $ret; } //插入数据方法 function insert($db,$sql){ $ret=mysqli_query($db,$sql); if($ret){ $ret = mysqli_insert_id($db); } return $ret; } //修改数据方法 function mod($db,$sql){ $ret=mysqli_query($db,$sql); return $ret; } //查询数据方法 function select($db,$table,$where,$order){ $sql="SELECT * FROM ".$table.' WHERE '.$where; if($order){ $sql .=' ORDER BY '.$order; } $sql .=' LIMIT 5 '; $ret=mysqli_query($db,$sql); $return =mysqli_fetch_assoc($ret); return $return ; }
<?php /** * Created by PhpStorm. * config.php 配置文件 * Date: 2019/5/6 * Time: 20:30 */ //服务器配置 include "function.php"; $db=[ 'host'=>'127.0.0.1', 'user'=>'root', 'pass'=>'root', 'name'=>'phpsql' ];
经过本章节的学习对sql的知识又重新温习了一遍,sqli 操作数据库语法简捷,对数据表进行删、改操作时切记附上where条件判断,不然会全部受影响。退出数据库操作时记得关闭数据库连接,当查询操作完成后要对返回值进行内存释放。
Correcting teacher:查无此人Correction time:2019-05-07 09:41:41
Teacher's summary:完成的不错。以后应该会使用pdo查询的多。mysqli也要了解,毕竟公司也有很多老项目。继续加油。