簡單的php Mysql類(查詢 刪除 更新)

來源:互聯網
上載者:User
 代碼如下 複製代碼
class mysql
{
    private $host;
    private $user;
    private $pass;
    private $database;
    private $charset;
    function __construct($host,$user,$pass,$database,$charset)
    {
        $this--->host=$host;
        $this->user=$user;
        $this->pass=$pass;
        $this->database=$database;
        $this->charset=$charset;
        $this->connect();
    }
    private function connect()//串連函數
    {
        mysql_connect($this->host,$this->user,$this->pass) or die ("串連資料庫伺服器失敗!");
        mysql_select_db($this->database) or die ("串連資料庫失敗!");
        mysql_query("set names $this->charset");       
    }
    function select($sql,$tab,$col,$value)//選擇函數
    {
        $select=mysql_query("select $sql from $tab where $col=$value");
        $row=mysql_fetch_array($select);
        return $row;
    }
    function insert($tab,$col,$value)//插入資料函數
    {
        mysql_query("INSERT INTO $tab($col)values($value)");
    }
    function update($tab,$col,$new_value,$colm,$value)//更新資料函數
    {
        mysql_query("UPDATE $tab SET $col=$new_value where $colm=$value");   
    }
    function delete($tab,$col,$value)//刪除資料函數
    {
        mysql_query("DELETE FROM $tab where $col=$value");
        }
    function close()//關閉串連函數
    {
    mysql_close();
 
    }
}
//$mysql=new mysql("127.0.0.1","root","120360150","test","gbk");串連資料庫
//$sql=$mysql->select("*","admin","id","3");//選擇資料
//print_r($sql);//列印返回的數組
//$mysql->insert("admin","user_name,user_pass","'123','123'");插入資料
//$mysql->update("admin","user_pass","22","id","3");更新資料
//$mysql->delete("admin","id","4");刪除資料
//$mysql->close();關閉串連
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.