放到类中去
<?php
class Admin
{
public $mysqli;
function __construct()
{
$this->mysqli = new mysqli('localhost', 'root', 'eshop', 'ouk_wmdf');
}
function addItem()
{
$query = "insert into ....";
$this->mysqli->query($query);
}
}$o = new Admin;
$o->addItem();
?>
<?php
class Admin
{
public $mysqli;
function __construct()
{
$this->mysqli = new mysqli('localhost', 'root', 'eshop', 'ouk_wmdf');
}
function addItem()
{
$query = "insert into ....";
$this->mysqli->query($query);
}
}$o = new Admin;
$o->addItem();
?>
如果一个页面要调用许多这样的类那么不是要建好多连接?
{
public $mysqli;
function __construct( $DB )
{
$this->mysqli = $db;
}
function addItem()
{
$query = "insert into ....";
$this->mysqli->query($query);
}
}
$mysqli = new mysqli('localhost', 'root', 'eshop', 'ouk_wmdf');
$o = new Admin( $mysqlli );
$o->addItem();
?>