namespace IMooc;interface IDatebase{ function connect($host,$user,$password,$dbname); function query($sql); function close();}
namespace IMooc\Datebase;use IMooc\IDatebase;class MySQL implements IDatebase{ protected $conn; function connect($host, $user, $password, $dbname) { $conn = mysql_connect($host,$user,$password); mysql_select_db($dbname,$conn); $this->conn = $conn; } function query($sql) { return mysql_query($sql,$this->conn); } function close() { mysql_close($this->conn); }}
namespace IMooc\Datebase;use IMooc\IDatebase;class MySQLi implements IDatebase{ protected $conn; function connect($host, $user, $password, $dbname){ $this->conn = mysqli_connect($host, $user, $password, $dbname); } function query($sql){ return mysqli_query($this->conn,$sql); } function close(){ mysqli_close($this->conn); }}
namespace IMooc\Datebase;use IMooc\IDatebase;class PDO implements IDatebase{ protected $conn; function connect($host, $user, $password, $dbname){ $conn = new \PDO("mysql:host=$host;dbname=$dbname",$user,$password); $this->conn = $conn; } function query($sql) { return $this->conn->query($sql); } function close(){ unset($this->conn); }}