<?php class DB { const HOST='127.0.0.1'; const USER='root'; const PASS='root'; const DATA='mooc'; static public $_instance; private function __construct() { mysql_connect(DB::HOST,DB::USER,DB::PASS); mysql_set_charset('utf8'); 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!