我正在连接到一个localhost数据库,为此我创建了一个类:
<?php
class Database
{
private $host, $user, $pass, $db;
/**
* @var mysqli
*/
private $link;
public function __construct($host, $user, $pass, $db)
{
$this->host = $host; $this->user = $user;
$this->pass = $pass; $this->db = $db;
}
public function connect()
{
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->db);
if ($this->link->connect_errno > 0) die("Cannot connect to server.");
}
public function getLink()
{
return $this->link;
}
public function reconnect($host, $user, $pass, $db)
{
$this->host = $host; $this->user = $user;
$this->pass = $pass; $this->db = $db;
$this->connect();
}
}
我这样连接到它:
$database = new Database("localhost", "username", "password", "dbname");
$database->connect();
$mysqli = $database->getLink();
但当我var_dump
mysqli变量时,它给我的全部是null的完整列表,如下:
object(mysqli)[2]
public 'affected_rows' => null
public 'client_info' => null
public 'client_version' => null
public 'connect_errno' => null
public 'connect_error' => null
public 'errno' => null
public 'error' => null
public 'error_list' => null
public 'field_count' => null
public 'host_info' => null
public 'info' => null
public 'insert_id' => null
public 'server_info' => null
public 'server_version' => null
public 'stat' => null
public 'sqlstate' => null
public 'protocol_version' => null
public 'thread_id' => null
public 'warning_count' => null
为什么会这样?我之前没有做过多次不同的事情......
修改
var_dump
的 $database
:
object(Database)[1]
private 'host' => string 'localhost' (length=9)
private 'user' => string 'username' (length=4)
private 'pass' => string 'password' (length=0)
private 'db' => string 'dbname' (length=2)
private 'link' =>
object(mysqli)[2]
public 'affected_rows' => null
public 'client_info' => null
public 'client_version' => null
public 'connect_errno' => null
public 'connect_error' => null
public 'errno' => null
public 'error' => null
public 'error_list' => null
public 'field_count' => null
public 'host_info' => null
public 'info' => null
public 'insert_id' => null
public 'server_info' => null
public 'server_version' => null
public 'stat' => null
public 'sqlstate' => null
public 'protocol_version' => null
public 'thread_id' => null
public 'warning_count' => null