• 封装PHP增删改查方法


    <?php
    
    
    class sqlModel{
    public $db;
    public function __construct(){ try{ $dbms='mysql';//数据库类型 $dbName='dbname';//使用的数据库 $user='root';//数据库连接用户名 $pwd='root';//数据库连接密码 $host='localhost';//数据库主机名 $port = '3306';//端口 $dsn = "mysql:host=$host;dbname=$dbName"; $this->db = new PDO($dsn,$user,$pwd);//数据库连接 }catch(Exception $e){ die("Error!:".$e->getMessage().'<br>'); } }

         //增加
    public function add($table,$str1,$str2){ $sql = "insert into ".$table." set name=:xuesheng,age=:yuwen"; $db = $this->db; $db->query('set names utf8'); $row = $db->prepare($sql); $res = $row->execute($str1);//数组的格式 $str1[':xuesheng']='xuesheng', $data[':yuwen'] = 'yuwen'; return $row; }
         //删除
    public function del($table){ $sql = "delete from ".$table.' where id = :id'; $db = $this->db; $row = $db->prepare($sql); $res = $row->execute($str1); return $row; }
         //修改
    public function update($table,$str1){ $sql = "UPDATE ".$table. " set name=:xuesheng,age=:yuwen where id = :id"; $db = $this->db; $row = $db->prepare($sql); $res = $row->execute($str1); return $row; }
         //获取一条
    public function getOne($table,$id){ $sql = "select * from ".$table; $db = $this->db; $data = $db->query($sql); $row = $data->fetch(PDO::FETCH_ASSOC); return $row; }
    //获取所有
    public function getAll($table){ $sql = "select * from ".$table; $db = $this->db; $data = $db->query($sql); while($row = $data->fetch(PDO::FETCH_ASSOC)){ $arr[] = $row; } return $arr; } } ?>

    调用方法:

    <?php
    
    
    require "demo.php";//把刚才的文件加载进来
    
    $dbsql = new sqlModel();
    $res = $dbsql->add('jh_ceshi',array(':xuesheng'=>'ceshi',':yuwen'=>213,':id'=>6,'');
    $res = $dbsql->del('jh_ceshi',array(':id'=>6);
    $res = $dbsql->update('jh_ceshi',array(':xuesheng'=>'ceshi',':yuwen'=>213,':id'=>6,'');
    $res = $dbsql->getOne('jh_ceshi',6,'');
    $res = $dbsql->getAll('jh_ceshi');
    
    ?>

    写的不好,请大家赐教。

    本文为原创博文,未经允许不得转载,转载请标明出处。

  • 相关阅读:
    FOAT、SOAT、OAT
    autosar mcal之启动代码
    华为2020操作系统相关链接openeuler
    CCP/XCP
    autosar资料(持续更新。。。)
    liteos相关源码资料
    autosar学习之通信栈
    Autosar COM层发送模式选择(信号发送属性和I-PDU发送模式)
    统一诊断服务 (Unified diagnostic services , UDS)
    Linux路由器及交换机工作原理
  • 原文地址:https://www.cnblogs.com/phpxj/p/10021328.html
Copyright © 2020-2023  润新知