《PHP編程最快明白》第六講:Mysql數(shù)據(jù)庫操作

字號:


    如果一行代碼搞不定你要的功能,該怎么辦?
    答案就是做成一個類--數(shù)據(jù)庫類就產(chǎn)生了。通過對函數(shù)的二次封裝,實現(xiàn)了非常好的重用。要用的時候再include進去。
    在講PHP數(shù)據(jù)庫之前,先介紹一下Mysql要點:大家可以用phpmyadmin學習數(shù)據(jù)庫操作。
    在phpmyadmin里看到編碼這一項全部選中文utf-8就對了。
    Mysql數(shù)據(jù)庫類型主要是:char(固定空間字符串,多大就是多少個中文字符)、varchar(可變空間字符串,多大就是初始化多少個中文字符)、int(整數(shù)多大就是多少位)、float(浮點數(shù))、timestamp(日期,可選建立時自動創(chuàng)建,輸出時就已經(jīng)是格式化過的date)、text(文本)、bool(布爾型)
    寫sql語句時SUM()可以統(tǒng)計值;orderby'id'DESCLIMIT10,10等要活用。
    在phpmyadmin學一下sql語句增刪改查就行了。
    實例20Mysql類
    代碼如下:
    <?php
    classopmysql{
    private$host='localhost';//服務器地址
    private$name='root';//登錄賬號
    private$pwd='';//登錄密碼
    private$dBase='a0606123620';//數(shù)據(jù)庫名稱
    private$conn='';//數(shù)據(jù)庫鏈接資源
    private$result='';//結果集
    private$msg='';//返回結果
    private$fields;//返回字段
    private$fieldsNum=0;//返回字段數(shù)
    private$rowsNum=0;//返回結果數(shù)
    private$rowsRst='';//返回單條記錄的字段數(shù)組
    private$filesArray=array();//返回字段數(shù)組
    private$rowsArray=array();//返回結果數(shù)組
    private$idusername=array();
    private$idsubtitle=array();
    //初始化類
    function__construct($host='',$name='',$pwd='',$dBase=''){
    if($host!='')
    $this->host=$host;
    if($name!='')
    $this->name=$name;
    if($pwd!='')
    $this->pwd=$pwd;
    if($dBase!='')
    $this->dBase=$dBase;
    $this->init_conn();
    }
    //鏈接數(shù)據(jù)庫
    functioninit_conn(){
    $this->conn=@mysql_connect($this->host,$this->name,$this->pwd);
    @mysql_select_db($this->dBase,$this->conn);
    mysql_query("setnamesutf8");
    }
    //查詢結果
    functionmysql_query_rst($sql){
    if($this->conn==''){
    $this->init_conn();
    }
    $this->result=@mysql_query($sql,$this->conn);
    }
    //取得查詢結果字段數(shù)目
    functiongetFieldsNum($sql){
    $this->mysql_query_rst($sql);
    $this->fieldsNum=@mysql_num_fields($this->result);
    }
    //取得查詢結果行數(shù)目
    functiongetRowsNum($sql){
    $this->mysql_query_rst($sql);
    if(mysql_errno()==0){
    return@mysql_num_rows($this->result);
    }else{
    return'';
    }
    }
    //取得記錄數(shù)組有索引(單條記錄)
    functiongetRowsRst($sql){
    $this->mysql_query_rst($sql);
    if(mysql_error()==0){
    $this->rowsRst=mysql_fetch_array($this->result,MYSQL_ASSOC);
    return$this->rowsRst;
    }else{
    return'';
    }
    }
    //取得記錄數(shù)組有索引(多條記錄)全部
    functiongetRowsArray($sql){
    $this->mysql_query_rst($sql);
    if(mysql_errno()==0){
    while($row=mysql_fetch_array($this->result,MYSQL_ASSOC)){
    $this->rowsArray[]=$row;
    }
    return$this->rowsArray;
    }else{
    return'';
    }
    }
    //更新、刪除、添加記錄數(shù),返回影響到的行數(shù)
    functionuidRst($sql){
    if($this->conn==''){
    $this->init_conn();
    }
    @mysql_query($sql);
    $this->rowsNum=@mysql_affected_rows();
    if(mysql_errno()==0){
    return$this->rowsNum;
    }else{
    return'';
    }
    }
    //獲取對應的字段值,一條數(shù)字索引,mysql_array_rows才是帶字段索引
    functiongetFields($sql,$fields){
    $this->mysql_query_rst($sql);
    if(mysql_errno()==0){
    if(mysql_num_rows($this->result)>0){
    $tmpfld=@mysql_fetch_row($this->result);
    $this->fields=$tmpfld[$fields];
    }
    return$this->fields;
    }else{
    return'';
    }
    }
    //錯誤信息
    functionmsg_error(){
    if(mysql_errno()!=0){
    $this->msg=mysql_error();
    }
    return$this->msg;
    }
    //釋放結果集
    functionclose_rst(){
    mysql_free_result($this->result);
    $this->msg='';
    $this->fieldsNum=0;
    $this->rowsNum=0;
    $this->filesArray='';
    $this->rowsArray='';
    $this->idsubtitle='';
    $this->idusername='';
    }
    //關閉數(shù)據(jù)庫
    functionclose_conn(){
    $this->close_rst();
    mysql_close($this->conn);
    $this->conn='';
    }
    }
    ?>
    實例21類的使用、密碼的md5加密
    代碼如下:
    <?php
    $conne=newopmysql();
    $conne->getRowsArray($sql);
    $conne->close_conn();
    $password=”123456一二三四五六”;
    echomd5($password.”www.***.com”);//輸出為32位的密文,是沒有解密函數(shù)的,可以實現(xiàn)簡單的加密功能。
    ?>