同时支持三个mysql+sqlite+pdo的php数据库类_同时支持三个MySQL+SQLite+PDO的PHP数据库类...

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

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

// mysql connect

$db = new SQL('mysql:host=localhost;database=21andy_blog;', '21andy.com_user', '21andy.com_password');

// PDO SQLite3 connect

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

// SQLite2 connect

$db = new SQL('sqlite:database=/21andy.com/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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值