A simple DAO class based on PHP and MySQL implements the CRUD operations _php instance

Source: Internet
Author: User
Tags php and php and mysql

Copy Code code as follows:

<?php
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. " <br> ";
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. " <br> ";
$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. " <br> ";
$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. " <br> ";
            $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);
}
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.