Home > php教程 > PHP源码 > php 类的写法

php 类的写法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:30:32
Original
1423 people have browsed it
<script>ec(2);</script>

php 类的写法//Db.class.php
class Db {
    private $_dblink;
    public $result;
    public function __construct($host, $user, $password, $dbname, $charset = 'utf8') {
        try{
            if($this->_dblink = mysql_connect($host, $user, $password)) {
                if(mysql_select_db($dbname)) {
                    $this->query("SET NAMES " . $charset . " ;");
                } else {
                    throw new Exception(mysql_error());
                }
            } else {
                throw new Exception(mysql_error());
            }
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }

    public function query($sql) {
        return $this->result = mysql_query($sql);
    }

    public function fetch() {
        return mysql_fetch_array($this->result);
    }

    public function fetchAll() {
        $rs = array();
        $rsAll = array();
        while($rs = mysql_fetch_array($this->result)) {
            $rsAll[] = $rs;
        }
        return $rsAll;
    }

    public function __destruct() {
        mysql_close($this->_dblink);
    }
}


复制PHP内容到剪贴板PHP代码:
//test.php
$db = new Db('localhost', 'root', '', 'hent_qxoa');
$db->query("SELECT * FROM qx_user ;");
var_dump($db->fetchAll());

 

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template