会员登录 | 会员注册 | 意见建议 | 网站地图

站长资源综合门户

当前位置:首页 > 站长学院 > 编程程序 > 同时支持三个MySQL+SQLite+PDO的PHP数据库类

同时支持三个MySQL+SQLite+PDO的PHP数据库类

时间:2012-08-16 20:39:14   作者:   来源:   点击:

 

PHP学习教程文章简介: 同时支持三个MySQL+SQLite+PDO的PHP数据库类使用方法: // mysql connect $db = new SQL(mysql:host=localhost;database=21andy_blog;, 21andy_user, 21andy_password); // PDO SQLite3 connect $db = new SQL(pdo:database=/21andy/21andy.s

同时支持三个MySQL+SQLite+PDO的PHP数据库类使用方法:

// mysql connect

$db = new SQL('mysql:host=localhost;database=21andy_blog;', '21andy_user', '21andy_password');

// PDO SQLite3 connect

$db = new SQL('pdo:database=/21andy/21andy.sqlite3;');

// SQLite2 connect

$db = new SQL('sqlite:database=/21andy/21andy.sqlite;');

sqldbs.class.php文件

/*

SQL Buddy - Web based MySQL administration

sqldbs.class.php

- sql class

MIT license

*/

class SQL {

var $adapter = "";

var $method = "";

var $version = "";

var $conn = "";

var $options = "";

var $errorMessage = "";

var $db = "";

function SQL($connString, $user = "", $pass = "") {

list($this->adapter, $options) = explode(":", $connString, 2);

if ($this->adapter != "sqlite") {

$this->adapter = "mysql";

}

$optionsList = explode(";", $options);

foreach ($optionsList as $option) {

list($a, $b) = explode("=", $option);

$opt[$a] = $b;

}

$this->options = $opt;

$database = (array_key_exists("database", $opt)) ? $opt['database'] : "";

if ($this->adapter == "sqlite" && substr(sqlite_libversion(), 0, 1) == "3" && class_exists("PDO") && in_array("sqlite", PDO::getAvailableDrivers())) {

$this->method = "pdo";

try

{

$this->conn = new PDO("sqlite:" . $database, null, null, array(PDO::ATTR_PERSISTENT => true));

}

catch (PDOException $error) {

$this->conn = false;

$this->errorMessage = $error->getMessage();

}

} else if ($this->adapter == "sqlite" && substr(sqlite_libversion(), 0, 1) == "2" && class_exists("PDO") && in_array("sqlite2", PDO::getAvailableDrivers())) {

$this->method = "pdo";

try

{

$this->conn = new PDO("sqlite2:" . $database, null, null, array(PDO::ATTR_PERSISTENT => true));

}

catch (PDOException $error) {

$this->conn = false;

$this->errorMessage = $error->getMessage();

}

} else if ($this->adapter == "sqlite") {

$this->method = "sqlite";

$this->conn = sqlite_open($database, 0666, $sqliteError);

} else {

$this->method = "mysql";

$host = (array_key_exists("host", $opt)) ? $opt['host'] : "";

$this->conn = @mysql_connect($host, $user, $pass);

}

if ($this->conn && $this->method == "pdo") {

$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);

}

if ($this->conn && $this->adapter == "mysql") {

$this->query("SET NAMES 'utf8'");

}

if ($this->conn && $database) {

$this->db = $database;

}

}

function isConnected() {

return ($this->conn !== false);

}

function close() {

return $this->disconnect();

}

function disconnect() {

if ($this->conn) {

if ($this->method == "pdo") {

$this->conn = null;

} else if ($this->method == "mysql") {

mysql_close($this->conn);

$this->conn = null;

} else if ($this->method == "sqlite") {

sqlite_close($this->conn);

$this->conn = null;

}

}

}

function getAdapter() {

return $this->adapter;

}

function getMethod() {

return $this->method;

}

function getOptionValue($optKey) {

if (array_key_exists($optKey, $this->options)) {

return $this->options[$optKey];

} else {

return false;

}

}

function selectDB($db) {

if ($this->conn) {

if ($this->method == "mysql") {

$this->db = $db;

return (mysql_select_db($db));

} else {

return true;

}

} else {

return false;

}

}

function query($queryText) {

if ($this->conn) {

if ($this->method == "pdo") {

$queryResult = $this->conn->prepare($queryText);

if ($queryResult)

$queryResult->execute();

分享到:

网友评论

热门编程程序