• 技术文章 >后端开发 >php教程

    基于php和mysql的简单的dao类实现crud操作功能_php实例

    2016-06-07 17:22:14原创377
    复制代码 代码如下:

    //require_once('FirePHPCore/FirePHP.class.php');
    //$firephp = FirePHP::getInstance(true); // debugger in firefox
    class SimpleDao {
    private $_table = null;
    private static $_con = null;

    public function SimpleDao() {
    if ($this->_con == null) {
    $this->_con = @mysql_connect("localhost", "root", "123456");
    if ($this->_con == FALSE) {
    echo("connect to db server failed.");
    $this->_con = null;
    return;
    }
    //$firephp->log("new DAO object");
    @mysql_select_db("swan", $this->_con);
    }
    }

    public function table($tablename) {
    $this->_table = $tablename;
    return $this;
    }

    public function query($sql) {
    $result = @mysql_query($sql);
    $ret = [];
    if ($result) {
    while ($row = mysql_fetch_array($result)) {
    $ret[] = $row;
    }
    }
    return $ret;
    }

    public function get($where = null) {
    $sql = "select * from ".$this->_table;
    $sql = $sql.$this->_getWhereString($where);
    //echo "[get]".$sql."
    ";
    return $this->query($sql);
    }

    public function insert($params) {
    if ($params == null || !is_array($params)) {
    return -1;
    }
    $keys = $this->_getParamKeyString($params);
    $vals = $this->_getParamValString($params);
    $sql = "insert into ".$this->_table."(".$keys.") values(".$vals.")";
    //echo "[insert]".$sql."
    ";
    $result = @mysql_query($sql);
    if (! $result) {
    return -1;
    }
    return @mysql_insert_id();
    }

    public function update($params, $where = null) {
    if ($params == null || !is_array($params)) {
    return -1;
    }
    $upvals = $this->_getUpdateString($params);
    $wheres = $this->_getWhereString($where);
    $sql = "update ".$this->_table." set ".$upvals." ".$wheres;
    //echo "[update]".$sql."
    ";
    $result = @mysql_query($sql);
    if (! $result) {
    return -1;
    }
    return @mysql_affected_rows();
    }

    public function delete($where) {
    $wheres = $this->_getWhereString($where);
    $sql = "delete from ".$this->_table.$wheres;
    //echo "[delete]".$sql."
    ";
    $result = @mysql_query($sql);
    if (! $result) {
    return -1;
    }
    return @mysql_affected_rows();
    }

    protected function _getParamKeyString($params) {
    $keys = array_keys($params);
    return implode(",", $keys);
    }

    protected function _getParamValString($params) {
    $vals = array_values($params);
    return "'".implode("','", $vals)."'";
    }

    private function _getUpdateString($params) {
    //echo "_getUpdateString";
    $sql = "";
    if (is_array($params)) {
    $sql = $this->_getKeyValString($params, ",");
    }
    return $sql;
    }

    private function _getWhereString($params) {
    //echo "_getWhereString";
    $sql = "";
    if (is_array($params)) {
    $sql = " where ";
    $where = $this->_getKeyValString($params, " and ");
    $sql = $sql.$where;
    }
    return $sql;
    }

    private function _getKeyValString($params, $split) {
    $str = "";
    if (is_array($params)) {
    $paramArr = array();
    foreach($params as $key=>$val) {
    $valstr = $val;
    if (is_string($val)) {
    $valstr = "'".$val."'";
    }
    $paramArr[] = $key."=".$valstr;
    }
    $str = $str.implode($split, $paramArr);
    }
    return $str;
    }

    public function release() {
    @mysql_close();
    }
    }

    function T($table) {
    return (new SimpleDao())->table($table);
    }
    ?>

    php入门到就业线上直播课:查看学习

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    千万级数据并发解决方案(理论+实战):点击学习

    Mysql单表千万级数据量的查询优化与性能分析

    Mysql主从原理及其在高并发系统中的应用

    专题推荐:mysql dao crud操作
    上一篇:php利用单例模式实现日志处理类库_php实例 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 你知道如何用PHP实现多进程吗• PHP与MySQL连接的方法总结• 求解:phpcms模板怎样转码?该怎么解决• php 之 cookie 跟 session 简单解读(笔记)• php 网页截取快讯代码
    1/1

    PHP中文网