Home >Backend Development >PHP Tutorial >PHP universal database connection method

PHP universal database connection method

一个新手
一个新手Original
2017-10-04 09:27:461490browse


<?php class DB {
    const HOST=&#39;127.0.0.1&#39;;
    const USER=&#39;root&#39;;
    const PASS=&#39;root&#39;;
    const DATA=&#39;mooc&#39;;
    static public $_instance;
    private function __construct() {
    mysql_connect(DB::HOST,DB::USER,DB::PASS);
    mysql_set_charset(&#39;utf8&#39;);
    mysql_select_db(DB::DATA);
}
public static function getInstance() {
    if(!self::$_instance instanceof self) {
    self::$_instance=new self;
}
return self::$_instance;
}
} $model=DB::getInstance();
    //连接数据库,并且测试 $sql="select * from user";
    $res=mysql_query($sql);
    while($row=mysql_fetch_assoc($res)) {
    var_dump($row);
}
?>

The above is the detailed content of PHP universal database connection method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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