<?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); } ?>
Atas ialah kandungan terperinci PHP万能连接数据库方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!