连接.php
<?php
class database
{
public $host = "localhost";
public $user = "root";
public $pass = "";
public $db = "db";
public $link;
public function __construct()
{
$this->connect();
}
private function connect()
{
$this->link = new mysqli($this->host, $this->user, $this->pass, $this->db);
return $this->link;
}
public function select($query)
{
$result = $this->link->query($query) or die($this->link->error.__LINE__);
if($result->num_rows > 0)
{
return $result;
}
else
{
return false;
}
}
?>
现在 action.php where 可以进行如下查询:
<?php include 'database.php'; ?>
$db = new database();
$query = "SELECT COUNT(*) FROM accounts";
$row = $db->select($query)->num_rows;
echo $row;
?>
完整的 conncetion.php :链接