现在我有一个配置文件如下:
<?php
return array(
'server'=>'localhost',
'username'=>'root',
'password'=>'123456',
'dbname'=>'duxiu',
'charset'=>'utf8'
);
我对使用以下两种方法连接 mysql 所花费的时间感到困惑:
1.
<?php
class Mysql{
private $conn;
public function __construct($c){
$this->conn=mysql_connect($c['server'],$c['username'],$c['password'],true) or die("连接出错");
mysql_select_db($c['dbname'],$this->conn);
if(isset($c['charset'])){
mysql_query("set names ".$c['charset'],$this->conn);
}
}
}
$c=require('config.php');
var_dump($c);
$db=new Mysql($c);
它花费了 1.012 秒。
2.当我使用如下:
<?php
class Mysql{
private $conn;
public function __construct($c){
$this->conn=mysql_connect($c['server'],$c['username'],$c['password'],true) or die("connect error");
mysql_select_db($c['dbname'],$this->conn);
if(isset($c['charset'])){
mysql_query("set names ".$c['charset'],$this->conn);
}
}
}
$c=array('server'=>'127.0.0.1','username'=>'root','password'=>'123456','dbname'=>'duxiu','charset'=>'utf8');
var_dump($c);
$db=new Mysql($c);
现在它花费了 0.012 秒。
为什么?