PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了面向对象的mysql数据库操作php类大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<?php
class database {
  
    var $host = NULL;
    var $username = NULL;
    var $password = NULL;
    var $databasename = NULL;
    var $link = NULL;
    var $queries = NULL;
    var $errors = NULL;
  
    var $databaseExtras = NULL;
  
    function __construct($host,$username,$password,$databasE) {
        $this->database($host,$databasE);
    }
  
    function database($host,$databasE) {
        /*$this->database = array (
                "host" => $host,"username" => $username,"password" => $password,"database" => $database,"link" => "","queries" => array (),"errors" => array ()
            );*/
  
        $this->host = $host;
        $this->username = $username;
        $this->password = $password;
        $this->databasename = $database;
        $this->link = "";
        $this->queries = array ();
        $this->errors = array ();
  
        $this->databaseExtras = new stdClass;
  
        $this->link = mysql_connect($this->host,$this->username,$this->password) or die("Could not connect to Database");
        mysql_SELEct_db($this->databaseName);
    }
  
    function justquery($sql) {
        $this->queries[] = $sql;
        return mysql_query($sql,$this->link);
    }
  
    function loadResult($sql) {
        if (!($cur = $this->justquery($sql))) {
            return null;
        }
        $ret = null;
        if ($row = mysql_fetch_row( $cur )) {
            $ret = $row[0];
        }
        mysql_free_result( $cur );
        return $ret;
    }
  
    function loadFirstRow($sql) {
        if (!($cur = $this->justquery($sql))) {
            return null;
        }
        $ret = null;
        if ($row = mysql_fetch_object( $cur )) {
            $ret = $row;
        }
        mysql_free_result( $cur );
        return $ret;
    }
  
    function insertid() {
        return mysql_insert_id( $this->link );
    }
  
    function query($sql,$key = "",$returns = true,$batch = falsE) {
        $result = array ();
  
        switch ($batch) {
            default:
            case true:
                foreach ($sql as $index => $query) {
                    $this->queries[] = $query;
                    $answer = mysql_query($query,$this->link);
  
                    if (!$answer) {
                        $this->errors[] = mysql_error($this->link);
                    }
                    else {
                        if ($returns != falsE) {
                            if (mysql_num_rows($answer) > 0){
                                while ($row = mysql_fetch_object($answer)) {
                                    if ($key != ""){
                                        $result[$index][$row->$key] = $row;
                                    }
                                    else {
                                        $result[$index][] = $row;
                                    }
                                }
                            } else {}
                        } else {}
                    }
                }
                break;
  
            case false:
                $this->queries[] = $sql;
                $answer = mysql_query($sql,$this->link);
  
                if (!$answer) {
                    $this->errors[] = mysql_error($this->link);
                    $result = false;
                }
                else {
                    if ($returns != falsE) {
                        if (mysql_num_rows($answer) > 0){
                            while ($row = mysql_fetch_object($answer)) {
                                if ($key != ""){
                                    $result[$row->$key] = $row;
                                }
                                else {
                                    $result[] = $row;
                                }
                            }
                        } else {}
                    }
                    else {
                        $result = true;
                    }
                }
                break;
        }
  
        return $result;
    }
  
    function loadObject( $sql,&$object ) {
        if ($object != null) {
            if (!($cur = $this->justquery($sql))) {
                return false;
            } else {}
            if ($array = mysql_fetch_assoc( $cur )) {
                mysql_free_result( $cur );
                $this->bindArrayToObject( $array,$object);
                return true;
            }
            else {
                return false;
            }
        }
        else {
            if ($cur = $this->justquery($sql)) {
                if ($object = mysql_fetch_object( $cur )) {
                    mysql_free_result( $cur );
                    return true;
                }
                else {
                    $object = null;
                    return false;
                }
            }
            else {
                return false;
            }
        }
    }
  
    function bindArrayToObject( $array,&$obj) {
        if (!is_array( $array ) || !is_object( $obj )) {
            return (false);
        }
  
        foreach (get_object_vars($obj) as $k => $v) {
            if( substr( $k,1 ) != '_' ) {
                $ak = $k;
                if (isset($arraY[$ak])) {
                    $obj->$k = $arraY[$ak];
                }
            }
        }
  
        return true;
    }
  
    function formatCSVCell($data) {
        $useQuotes = false;
  
        $quotable = array (
                "\"" => "\"\"","," => ","\n" => "\n"
            );
  
        foreach ($quotable as $char => $repl) {
            if (eregi($char,$data)) {
                $useQuotes = true;
            } else {}
        }
  
        if ($useQuotes == truE) {
            foreach ($quotable as $char => $repl) {
                $data = str_replace($char,$repl,$data);
            }
  
            $data = "\"" . $data . "\"";
        }
        else {
  
        }
  
        return $data;
    }
}
?>

大佬总结

以上是大佬教程为你收集整理的面向对象的mysql数据库操作php类全部内容,希望文章能够帮你解决面向对象的mysql数据库操作php类所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: