同时支持三个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();
if (!$queryResult) {
$errorInfo = $this->conn->errorInfo();
$this->errorMessage = $errorInfo[2];
}
return $queryResult;
} else if ($this->method == "mysql") {
$queryResult = @mysql_query($queryText, $this->conn);
if (!$queryResult) {
$this->errorMessage = mysql_error();
}
return $queryResult;
} else if ($this->method == "sqlite") {
$queryResult = sqlite_query($this->conn, $queryText);
if (!$queryResult) {
$this->errorMessage = sqlite_error_string(sqlite_last_error($this->conn));
}
return $queryResult;
}
} else {
return false;
}
}
// Be careful using this function - when used with pdo, the pointer is moved
// to the end of the result set and the query needs to be rerun. Unless you
// actually need a count of the rows, use the isResultSet() function instead
function rowCount($resultSet) {
if (!$resultSet)
return false;
if ($this->conn) {
if ($this->method == "pdo") {
return count($resultSet->fetchAll());
} else if ($this->method == "mysql") {
return @mysql_num_rows($resultSet);
} else if ($this->method == "sqlite") {
return @sqlite_num_rows($resultSet);
}
}
}
function num_rows($res) {
return $this->rowCount($res);
}
function isResultSet($resultSet) {
if ($this->conn) {
if ($this->method == "pdo") {
return ($resultSet == true);
} else {
return ($this->rowCount($resultSet) > 0);
}
}
}
function fetch($res) {
return $this->fetchAssoc($res);
}
function fetchArray($resultSet) {
if (!$resultSet)
return false;
if ($this->conn) {
if ($this->method == "pdo") {
return $resultSet->fetch(PDO::FETCH_NUM);
} else if ($this->method == "mysql") {
return mysql_fetch_row($resultSet);
} else if ($this->method == "sqlite") {
return sqlite_fetch_array($resultSet, SQLITE_NUM);
}
}
}
function fetchAssoc($resultSet) {
if (!$resultSet)
return false;
if ($this->conn) {
if ($this->method == "pdo") {
return $resultSet->fetch(PDO::FETCH_ASSOC);
} else if ($this->method == "mysql") {
return mysql_fetch_assoc($resultSet);
} else if ($this->method == "sqlite") {
return sqlite_fetch_array($resultSet, SQLITE_ASSOC);
}
}
}
function affectedRows($resultSet) {
if (!$resultSet)
return false;
if ($this->conn) {
if ($this->method == "pdo") {
return $resultSet->rowCount();
} else if ($this->method == "mysql") {
return @mysql_affected_rows($resultSet);
} else if ($this->method == "sqlite") {
return sqlite_changes($resultSet);
}
}
}
function result($resultSet, $targetRow, $targetColumn = "") {
if (!$resultSet)
return false;
if ($this->conn) {
if ($this->method == "pdo") {
if ($targetColumn) {
$resultRow = $resultSet->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_ABS, $targetRow);
return $resultRow[$targetColumn];
} else {
$resultRow = $resultSet->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_ABS, $targetRow);
return $resultRow[0];
}
} else if ($this->method == "mysql") {
return mysql_result($resultSet, $targetRow, $targetColumn);
} else if ($this->method == "sqlite") {
return sqlite_column($resultSet, $targetColumn);
}
}
}
function listDatabases() {
if ($this->conn) {
if ($this->adapter == "mysql") {
return $this->query("SHOW DATABASES");
} else if ($this->adapter == "sqlite") {
return $this->db;
}
}
}
function listTables() {
if ($this->conn) {
if ($this->adapter == "mysql") {
return $this->query("SHOW TABLES");
} else if ($this->adapter == "sqlite") {
return $this->query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name");
}
}
}
function hasCharsetSupport()
{
if ($this->conn) {
if ($this->adapter == "mysql" && version_compare($this->getVersion(), "4.1", ">")) {
return true;
} else {
return false;
}
}
}
function listCharset() {
if ($this->conn) {
if ($this->adapter == "mysql") {
return $this->query("SHOW CHARACTER SET");
} else if ($this->adapter == "sqlite") {
return "";
}
}
}
function listCollation() {
if ($this->conn) {
if ($this->adapter == "mysql") {
return $this->query("SHOW COLLATION");
} else if ($this->adapter == "sqlite") {
return "";
}
}
}
function insertId() {
if ($this->conn) {
if ($this->method == "pdo") {
return $this->conn->lastInsertId();
} else if ($this->method == "mysql") {
return @mysql_insert_id($this->conn);
} else if ($this->method == "sqlite") {
return sqlite_last_insert_rowid($this-conn);
}
}
}
function escapeString($toEscape) {
if ($this->conn) {
if ($this->method == "pdo") {
$toEscape = $this->conn->quote($toEscape);
$toEscape = substr($toEscape, 1, -1);
return $toEscape;
} else if ($this->adapter == "mysql") {
return mysql_real_escape_string($toEscape);
} else if ($this->adapter == "sqlite") {
return sqlite_escape_string($toEscape);
}
}
}
function getVersion() {
if ($this->conn) {
// cache
if ($this->version) {
return $this->version;
}
if ($this->adapter == "mysql") {
$verSql = mysql_get_server_info();
$version = explode("-", $verSql);
$this->version = $version[0];
return $this->version;
} else if ($this->adapter == "sqlite") {
$this->version = sqlite_libversion();
return $this->version;
}
}
}
// returns the number of rows in a table
function tableRowCount($table) {
if ($this->conn) {
if ($this->adapter == "mysql") {
$countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table . "`");
$count = (int)($this->result($countSql, 0, "RowCount"));
return $count;
} else if ($this->adapter == "sqlite") {
$countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $table . "'");
$count = (int)($this->result($countSql, 0, "RowCount"));
return $count;
}
}
}
// gets column info for a table
function describeTable($table) {
if ($this->conn) {
if ($this->adapter == "mysql") {
return $this->query("DESCRIBE `" . $table . "`");
} else if ($this->adapter == "sqlite") {
$columnSql = $this->query("SELECT sql FROM sqlite_master where tbl_name = '" . $table . "'");
$columnInfo = $this->result($columnSql, 0, "sql");
$columnStart = strpos($columnInfo, '(');
$columns = substr($columnInfo, $columnStart+1, -1);
$columns = split(',[^0-9]', $columns);
$columnList = array();
foreach ($columns as $column) {
$column = trim($column);
$columnSplit = explode(" ", $column, 2);
$columnName = $columnSplit[0];
$columnType = (sizeof($columnSplit) > 1) ? $columnSplit[1] : "";
$columnList[] = array($columnName, $columnType);
}
return $columnList;
}
}
}
/*
Return names, row counts etc for every database, table and view in a JSON string
*/
function getMetadata() {
$output = '';
if ($this->conn) {
if ($this->adapter == "mysql" && version_compare($this->getVersion(), "5.0.0", ">=")) {
$this->selectDB("information_schema");
$schemaSql = $this->query("SELECT `SCHEMA_NAME` FROM `SCHEMATA` ORDER BY `SCHEMA_NAME`");
if ($this->rowCount($schemaSql)) {
while ($schema = $this->fetchAssoc($schemaSql)) {
$output .= '{"name": "' . $schema['SCHEMA_NAME'] . '"';
// other interesting columns: TABLE_TYPE, ENGINE, TABLE_COLUMN and many more
$tableSql = $this->query("SELECT `TABLE_NAME`, `TABLE_ROWS` FROM `TABLES` WHERE `TABLE_SCHEMA`='" . $schema['SCHEMA_NAME'] . "' ORDER BY `TABLE_NAME`");
if ($this->rowCount($tableSql)) {
$output .= ',"items": [';
while ($table = $this->fetchAssoc($tableSql)) {
if ($schema['SCHEMA_NAME'] == "information_schema") {
$countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table['TABLE_NAME'] . "`");
$rowCount = (int)($this->result($countSql, 0, "RowCount"));
} else {
$rowCount = (int)($table['TABLE_ROWS']);
}
$output .= '{"name":"' . $table['TABLE_NAME'] . '","rowcount":' . $rowCount . '},';
}
if (substr($output, -1) == ",")
$output = substr($output, 0, -1);
$output .= ']';
}
$output .= '},';
}
$output = substr($output, 0, -1);
}
} else if ($this->adapter == "mysql") {
$schemaSql = $this->listDatabases();
if ($this->rowCount($schemaSql)) {
while ($schema = $this->fetchArray($schemaSql)) {
$output .= '{"name": "' . $schema[0] . '"';
$this->selectDB($schema[0]);
$tableSql = $this->listTables();
if ($this->rowCount($tableSql)) {
$output .= ',"items": [';
while ($table = $this->fetchArray($tableSql)) {
$countSql = $this->query("SELECT COUNT(*) AS `RowCount` FROM `" . $table[0] . "`");
$rowCount = (int)($this->result($countSql, 0, "RowCount"));
$output .= '{"name":"' . $table[0] . '","rowcount":' . $rowCount . '},';
}
if (substr($output, -1) == ",")
$output = substr($output, 0, -1);
$output .= ']';
}
$output .= '},';
}
$output = substr($output, 0, -1);
}
} else if ($this->adapter == "sqlite") {
$output .= '{"name": "' . $this->db . '"';
$tableSql = $this->listTables();
if ($tableSql) {
$output .= ',"items": [';
while ($tableRow = $this->fetchArray($tableSql)) {
$countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $tableRow[0] . "'");
$rowCount = (int)($this->result($countSql, 0, "RowCount"));
$output .= '{"name":"' . $tableRow[0] . '","rowcount":' . $rowCount . '},';
}
if (substr($output, -1) == ",")
$output = substr($output, 0, -1);
$output .= ']';
}
$output .= '}';
}
}
return $output;
}
function error() {
return $this->errorMessage;
}
}

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

如何使用PHP和SQLite建立使用者登入系統在當今網路時代,使用者登入系統是許多網站和應用程式的基本功能之一。本文將介紹如何使用PHP和SQLite來建立一個簡單且強大的使用者登入系統。 SQLite是一個嵌入式資料庫引擎,它是一個零配置的、伺服器端的資料庫引擎。 PHP是一種流行的伺服器端腳本語言,它與SQLite結合使用可以創建出靈活且高效的使用者登入系統。透過以

C語言return的用法有:1、對於傳回值類型為void的函數,可以使用return語句來提前結束函數的執行;2、對於傳回值型別不為void的函數,return語句的作用是將函數的執行結果傳回給呼叫者;3、提前結束函數的執行,在函數內部,我們可以使用return語句來提前結束函數的執行,即使函數並沒有回傳值。

使用PHP和SQLite實現使用者權限和存取控制在現代的web應用程式中,使用者權限和存取控制是非常重要的一部分。透過正確的權限管理,可以確保只有經過授權的使用者能夠存取特定的頁面和功能。在本文中,我們將學習如何使用PHP和SQLite來實現基本的使用者權限和存取控制。首先,我們需要建立一個SQLite資料庫來儲存使用者和其權限的資訊。下面是簡單的使用者表和權限表的結構

PHP和SQLite:如何進行資料壓縮和加密在許多Web應用程式中,資料的安全性和儲存空間的利用率是非常重要的考慮因素。 PHP和SQLite是兩個非常廣泛使用的工具,本文將介紹如何使用它們來進行資料壓縮和加密。 SQLite是一種輕量級的嵌入式資料庫引擎,它沒有獨立的伺服器進程,而是直接與應用程式互動。 PHP是一種流行的伺服器端腳本語言,被廣泛用於建立動態

使用PHP和SQLite實現資料圖表和視覺化概述:隨著大數據時代的到來,資料圖表和視覺化成為了展示和分析資料的重要方式。在本文中,將介紹如何使用PHP和SQLite實現資料圖表和視覺化的功能。以一個實例為例,展示如何從SQLite資料庫讀取數據,並使用常見的數據圖表庫來展示數據。準備工作:首先,需要確保已經安裝了PHP和SQLite資料庫。如果沒有安裝,可

隨著網路的發展,部落格成為越來越人分享自己生活、知識和想法的平台。如果你也想創建一個自己的博客,那麼本文將介紹如何使用PHP和SQLite來創建一個簡單的博客。在確定需求在開始創建部落格之前,我們需要確定自己想要實現的功能。例如:建立部落格文章編輯部落格文章刪除部落格文章顯示部落格文章清單顯示部落格文章詳情使用者認證和權限控制安裝PHP和SQLite我們需要安裝PHP和S

PHP和SQLite:如何處理長連結和斷線重連引言:在Web開發中,PHP和SQLite是兩個常用的技術。然而,長連接和斷線重連是在使用PHP和SQLite時經常遇到的一些問題。本文將介紹如何在PHP中處理長連接和斷線重連的問題,並提供一些實例程式碼,以幫助開發者更好地理解和解決這些問題。一、長連接問題在使用PHP連接SQLite資料庫時,長連接(Persis
