MySQL物件 -- 自己寫的

CFCCFC
edited 十月 2013 in 原創軟體分享區
這是小弟自己設計的
^^"喜歡就請用吧
慢慢會增加其他Member Function..

<?php
class MySQL{
//Global variables
var $HOST, $UNAME, $UPWD, $DB;
var $dbconn;

//Member functions

//Constructor
function MySQL($host, $username, $userpwd, $database){
$this->HOST=$host;
$this->UNAME=$username;
$this->UPWD=$userpwd;
$this->DB=$database;
}

//Connection
function Conn(){
$this->dbconn=mysql_connect($this->HOST, $this->UNAME, $this->UPWD) or die("I could not connect to the database.");
mysql_select_db($this->DB,$this->dbconn);
}


//MySQL query
function Query($str){
return mysql_query($str,$this->dbconn);
}

//Close
function Close(){
mysql_close($this->dbconn);
}

//Drop Database
function DropDB($database){
return mysql_drop_db($database);
}
}
?>

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=1225

評論

  • edited 二月 2006
    不妨可以嘗試思考自動分頁、新增與更新等操作來延伸這個物件 :)
  • edited 二月 2006
    我也來share一下好了,當初是為了什麼目的開發的我也忘了 ├004┤
    不過一直使用到現在倒也變成一種習慣了 ├005┤
    class Entity
    {
        var $conn, $DBHOST, $DBNAME, $DBUSER, $DBPWD, $DBTABLE, $error, $sql, $effect_rows;
    
        function Entity($dbhost, $dbname, $dbuser, $dbpwd, $dbtable, $LAN = "Big5")
        {
            $this->DBTABLE = $dbtable;
            $this->DBHOST = $dbhost;
            $this->DBNAME = $dbname;
            $this->DBUSER = $dbuser;
            $this->DBPWD = $dbpwd;
            $this->error = "";
            if (! $this->conn = mysql_connect($this->DBHOST, $this->DBUSER, $this->DBPWD))
            {
                $this->error = mysql_error();
                return -1;
            }
            else
            {
                if (! mysql_select_db($this->DBNAME, $this->conn))
                {
                    $this->error = mysql_error($this->conn);
                    return -1;
                }
                else
                {
                    mysql_query("set NAMES '" . $LAN . "'", $this->conn);
                    return true;
                }
            }
        }
    
        function insert($Data)
        {
            $stmt = "";
            $field = "";
            if(is_array($Data))
            {
                for (reset($Data); $key=key($Data); next($Data))
                {
                    $field .= "`" . $key . "`, ";
                    $stmt .= "'" . $Data[$key] . "', ";
                }
                $stmt = "insert into `" . $this->DBTABLE . "` (" . substr($field, 0, -2) . ") values (" . substr($stmt, 0, -2) . ")";
                if (! $result = mysql_query($stmt, $this->conn))
                {
                    $this->sql = $stmt;
                    $this->error = mysql_error($this->conn);
                    return -1;
                }
                else
                {
                    $this->sql = $stmt;
                    return true;
                }
            }
            else
            {
                $this->error = "格式錯誤!";
                return -1;
            }
        }
    
        function multiinsert($fields, $Data)
        {
            $stmt = "";
            $field = "";
            if (is_array($fields))
            {
                for (reset($fields);list($key, $value)=each($fields);)
                {
                    $field .= "`" . $fields[$key] . "`, ";
                }
                $field = substr($field, 0, -2);
            }
            else
                $field = $fields;
            if(is_array($Data))
            {
                for (reset($Data);list($key, $value)=each($Data);)
                {
                    if (is_array($value))
                    {
                        $stmt .= "(";
                        for (reset($value);list($key1, $vv)=each($value);)
                            $stmt .= "'" . $vv . "', ";
                        $stmt = substr($stmt, 0, -2) . "), ";
                    }
                    else
                    {
                        $stmt .= "(" . $value . "), ";
                    }
                }
                $stmt = "insert into `" . $this->DBTABLE . "` (" . $field . ") values " . substr($stmt, 0, -2);
                if (! $result = mysql_query($stmt, $this->conn))
                {
                    $this->sql = $stmt;
                    $this->error = mysql_error($this->conn);
                    return -1;
                }
                else
                {
                    $this->sql = $stmt;
                    return true;
                }
            }
            else
            {
                $this->error = "格式錯誤!";
                return -1;
            }
        }
    
        function delete($cond, $limits = "0")
        {
            $stmt = "delete from " . $this->DBTABLE . " where $cond";
            if ($limits)
                $stmt .= " limit " . $limits;
            if (! $result = mysql_query($stmt, $this->conn))
            {
                $this->sql = $stmt;
                $this->error = mysql_error($this->conn);
                return -1;
            }
            else
            {
                $this->sql = $stmt;
                return mysql_affected_rows($this->conn);
            }
        }
    
        function update($Data, $cond)
        {
            $stmt = "update `" .  $this->DBTABLE . "` set ";
            if(is_array($Data))
            {
                for (reset($Data); $key=key($Data); next($Data))
                {
                    $stmt .= "`" . $key . "` = '" . $Data[$key] . "', ";
                }
                $stmt = substr($stmt, 0, -2) . " where " . $cond;
                if (! $result = mysql_query($stmt, $this->conn))
                {
                    $this->sql = $stmt;
                    $this->error = mysql_error($this->conn);
                    return -1;
                }
                else
                {
                    $this->sql = $stmt;
                    $this->effect_rows = mysql_affected_rows($this->conn);
                    return 1;
                }
            }
            else
            {
                $this->error = "資料格式錯誤!";
                return -1;
            }
        }
    
        function select($cond="", $option="", $fields="*")
        {
            $fields = ($fields) ? $fields : "*";
            if($cond != "")
                $stmt = "select $fields from " . $this->DBTABLE . " where $cond $option";
            else 
                $stmt = "select $fields from " . $this->DBTABLE . " $option";
            if(! $result = mysql_query($stmt, $this->conn))
            {
                $this->sql = $stmt;
                $this->error = mysql_error($this->conn);
                return -1;
            }
            else
            {
                $this->sql = $stmt;
                $data = array();
                while($answers = mysql_fetch_assoc($result))
                {
                    $data[] = null;
                    $data[sizeof($data) - 1] = $answers;
                }
                return $data;
            }
        }
    
        function count($cond = "")
        {
            if ($cond)
                $stmt = "select count(*) from " . $this->DBTABLE . " where $cond";
            else
                $stmt = "select count(*) from " . $this->DBTABLE;
            if ($result = mysql_query($stmt, $this->conn))
            {
                $this->sql = $stmt;
                $answers = mysql_fetch_row($result);
                return $answers[0];
            }
            else
            {
                $this->sql = $stmt;
                $this->error = mysql_error($this->conn);
                return -1;
            }
        }
    
        function execute($stmt)
        {
            if ($result = mysql_query($stmt, $this->conn))
            {
                $data = array();
                $this->sql = $stmt;
                $this->error = mysql_error($this->conn);
                if (@mysql_num_rows($result))
                {
                    while($answers = mysql_fetch_assoc($result))
                    {
                        $data[] = null;
                        $data[sizeof($data) - 1] = $answers;
                    }
                    if (sizeof($data) > 0)
                    {
                        return $data;
                    }
                }
                else
                {
                    $this->sql = $stmt;
                    return mysql_affected_rows($this->conn);
                }
            }
            else
            {
                $this->sql = $stmt;
                $this->error = mysql_error($this->conn);
                return -1;
            }
        }
    
        function close()
        {
            mysql_close($this->conn);
        }
    }
    
  • edited 二月 2006
    貼程式碼的時候可以用 code 標籤包住,這樣子縮排就會呈現囉 :)

    雞婆改了一下,感謝提供~
  • CFCCFC
    edited 二月 2006
    謝謝幫我修改><
    感激^^
Sign In or Register to comment.