标签:size 函数 int turn nec ack connect mysql select
定义:可以将截然不同的函数接口封装成统一的API$db = new IMooc\Database\MySQL();
//$db = new IMooc\Database\MySQLi();
//$db = new IMooc\Database\PDO();
$db->connect(‘127.0.0.1‘, ‘root‘, ‘root‘, ‘test‘);
$db->query("show databases");
$db->close();
<?php
namespace IMooc;
interface IDatabase
{
function connect($host, $user, $passwd, $dbname);
function query($sql);
function close();
}
<?php
namespace IMooc\Database;
use IMooc\IDatabase;
class MySQL implements IDatabase
{
protected $conn;
function connect($host, $user, $passwd, $dbname)
{
$conn = mysql_connect($host, $user, $passwd);
mysql_select_db($dbname, $conn);
$this->conn = $conn;
}
function query($sql)
{
$res = mysql_query($sql, $this->conn);
return $res;
}
function close()
{
mysql_close($this->conn);
}
}
<?php
namespace IMooc\Database;
use IMooc\IDatabase;
class MySQLi implements IDatabase
{
protected $conn;
function connect($host, $user, $passwd, $dbname)
{
$conn = mysqli_connect($host,$user,$passwd,$dbname);
$this->conn = $conn;
}
function query($sql)
{
return mysqli_query($this->conn, $sql);
}
function close()
{
mysqli_close($this->conn);
}
}
<?php
namespace IMooc\Database;
use IMooc\IDatabase;
class PDO implements IDatabase
{
protected $conn;
function connect($host, $user, $passwd, $dbname)
{
$conn = new \PDO("mysql:host=$host;dbname=$dbname", $user, $passwd);
$this->conn = $conn;
}
function query($sql)
{
return $this->conn->query($sql);
}
function close()
{
unset($this->conn);
}
}
标签:size 函数 int turn nec ack connect mysql select
原文地址:http://www.cnblogs.com/phonecom/p/045f3d736d4c8a29c10b32149f9d94f8.html