php_DBHelper
包含【CURD】 demo
<?php class DBHelper{ private $host="localhost"; private $userName="root"; private $pwd="root"; private $DBName="mytest"; /** * 获取数据 */ function GetQuery($sql){ $conn=new mysqli($this->host,$this->userName,$this->pwd,$this->DBName); if(!$conn){ echo "conn mysql error"; mysqli_close($conn); return; } mysqli_query($conn,"set names utf8"); $return=mysqli_query($conn,$sql); mysqli_close($conn); return $return; } /** * 增删修函数 */ function NOQuery($sql){ $conn=new mysqli($this->host,$this->userName,$this->pwd,$this->DBName); if(!$conn){ echo "conn mysql error"; mysqli_close($conn); return; } mysqli_query($conn,"set names utf8"); mysqli_query($conn,$sql); //获取操作行数 $rows=mysqli_affected_rows($conn); mysqli_close($conn); return $rows;//别忘记$符号 } } #开始执行测试 $db=new DBHelper(); #添加数据 // $db->NOQuery("insert into users values(0,'damo','11','易筋经')"); #修改数据 // $db->NOQuery("update users set userName='达摩' where userName='damo'"); #删除数据 // $db->NOQuery("delete from users where userName='达摩'"); $result=$db->GetQuery("select * from users"); ?> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <table class="table table-hover table-bordered"> <tr class="info"> <th>编号</th> <th>用户名</th> <th>密码</th> <th>爱好</th> </tr> <?php foreach ($result as $rows) {?> <tr> <td><?php echo $rows["id"]?></td> <td><?php echo $rows["userName"]?></td> <td><?php echo $rows["passWord"]?></td> <td><?php echo $rows["likes"]?></td> </tr> <?php }?> </table>