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

PHP universal database connection method

一个新手
Release: 2023-03-16 13:14:01
Original
1480 people have browsed it


<?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);
}
?>
Copy after login

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!

Related labels:
source:php.cn
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template