+ -
当前位置:首页 → 问答吧 → mysql操作类,方便升级更换为orcale

mysql操作类,方便升级更换为orcale

时间:2008-02-25

来源:互联网

整天分享大家的代码我也发一个简单的mysql类吧。这个类只主要考虑以后数据库升级为orcale写的。功能不怎么样,大家只当拿来玩玩吧!
<?php
/*
learsu 070631
control mysql data base

$connection is open mysql connection
$result is query sql    0:false 1:successful
*/

class DBcontrol{
        protected $db_host        = "172.16.0.240";        //host ip
        protected $db_user        = "root";        //db username
        protected $db_password        = "123456";        //password
        protected $db_dbname        = "b2b_240";        //datebase name
        protected $connection        = 0;                //object coneection
        protected $result        = 0;                //result of sql query

        function __construct()
        {
                $this->connection();
                $this->select_db();
        }

        function __destruct()
        {
                /*mysql_free_result($this->result);        //this line will get a Warning  */
                $this->close();
                unset($this->db_host);
                unset($this->db_user);
                unset($this->db_password);
                unset($this->db_dbname);
                unset($this->connection);
                unset($this->result);
        }

        protected function connection()
        {
                $this->connection = mysql_connect($this->db_host, $this->db_user, $this->db_password);
                if (!$this->connection)
                {
                        echo "please check you database,connection is failed!";
                        exit();
                }
        }
                protected function select_db()
        {
                if (!mysql_select_db($this->db_dbname, $this->connection))
                {
                        echo "please check you dataname, select dataname is failed!";
                        exit();
                }
        }

        function query($types, $table_name, $table_item, $conditions, $table_values = "")
        {
                switch ($types)
                {
                        case "update":
                                $query = $this->updates($table_name, $table_item, $conditions);
                        return $query;
                        break;
                        case "insert":
                                $query = $this->inserts($table_name, $table_item, $table_values);
                        return $query;
                        break;
                        case "select":
                                $query = $this->selects($table_name, $table_item, $conditions);
                        return $query;
                        break;
                        case "count":
                                $query = $this->counts($table_name, $conditions);
                        return $query;
                        break;
                        case "delete":
                                $query = $this->deletes($table_name, $conditions);
                        return $query;
                        break;
                        case "check":
                                $query = $this->checks($table_name, $table_item, $conditions);
                        return $query;
                        break;
                }
        }

        function updates($table_name, $table_item, $conditions)
        {
                $sql = "update $table_name set $table_item where $conditions";
                $this->result = mysql_query($sql, $this->connection);
                if (!$this->result)
                {
                        return 0;
                }
                else
                {
                        return 1;
                }
        }

        function inserts($table_name, $table_item, $table_values)
        {
                $sql = "insert into $table_name ($table_item) values ($table_values)";
                $this->result = mysql_query($sql, $this->connection);
                if (!$this->result)
                {
                        return 0;
                }
                else
                {
                        return 1;
                }
        }

        function selects($table_name, $table_item, $conditions)
        {
                $sql = "select $table_item from $table_name where $conditions";
                $i = 0;
                $result_arr = array();
                $this->result = mysql_query($sql, $this->connection);
                if (!$this->result)
                {
                        $result_arr = 0;
                }
                else
                {
                        if (mysql_num_rows($this->result) == 0)
                        {
                                $result_arr = 0;
                        }
                        else
                        {
                                while($row = mysql_fetch_array($this->result))
                                {
                                        $result_arr[$i++] = $row;
                                }
                        }
                }
                return $result_arr;
        }

        function counts($table_name, $conditions)
        {
                $sql = "select count(*) from $table_name where $conditions";
                $this->result = mysql_query($sql, $this->connection);
                if (!$this->result)
                {
                        return 0;
                }
                else
                {
                        return mysql_num_rows($this->result);
                }
        }

        function deletes($table_name, $conditions)
        {
                $sql = "delete from $table_name where $conditions";
                $this->result = mysql_query($sql, $this->connection);
                if (!$this->result)
                {
                        return 0;
                }
                else
                {
                        return 1;
                }
        }

        function checks($table_name, $table_item, $conditions)
        {
                $sql = "select $table_item from $table_name where $conditions";
                $this->result = mysql_query($sql, $this->connection);
                $num_rows = mysql_num_rows($this->result);
                if ($num_rows == 0)
                {
                        return 0;
                }
                else
                {
                        return 1;
                }
        }
                // total nums of files
        function num_fields()
        {
                return mysql_num_fields($this->result);
        }

        // insert id
        function insert_id()
        {
                return mysql_insert_id($this->connection);
        }
                //fetch_rows
        function fetch_rows()
        {
                return mysql_affected_rows($this->connection);
        }

        // close connection
        function close()
        {
                mysql_close($this->connection);
        }
}

/*
$connection = new  DBcontrol();
$check_username = $connection->query("check", "f_account", "f_id", " f_username='learsu0'");
echo $check_username."++++++++";
*/
?>

[ 本帖最后由 learsu 于 2008-2-25 11:51 编辑 ]

作者: learsu   发布时间: 2008-02-25

作者: PHPChina   发布时间: 2008-02-25

不错,支持原创

作者: luzhou   发布时间: 2008-02-25

不错,支持

作者: RICHARD   发布时间: 2008-02-25

不错,支持一个。
个人认为,还是留一个可以让用户自己执行SQL的接口比较合理。
可以对付特殊需求嘛。

作者: adleyliu   发布时间: 2008-02-26

感谢yagas的建议。当时还真没考虑到这一点。

作者: yagas   发布时间: 2008-02-26

热门下载

更多