前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php_DBHelper

php_DBHelper

作者头像
红目香薰
发布2022-11-29 17:28:43
2200
发布2022-11-29 17:28:43
举报
文章被收录于专栏:CSDNToQQCode

php_DBHelper

包含【CURD】 demo

代码语言:javascript
复制
<?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>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-07,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • php_DBHelper
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档