热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

PHP学习教程文章简介:同时支持三个MySQL+SQLite+PDO的PHP数据库类使用方法://mysqlconnect$db=newSQL(mysql:host=localhost;database=21andy_blog;,21andy.com_user,21andy.com_passwo

 

  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;

  }

  }


推荐阅读
  • Issue with the Reserved Term HOSTS in System Configuration ... [详细]
  • Spring Boot 实战(一):基础的CRUD操作详解
    在《Spring Boot 实战(一)》中,详细介绍了基础的CRUD操作,涵盖创建、读取、更新和删除等核心功能,适合初学者快速掌握Spring Boot框架的应用开发技巧。 ... [详细]
  • 如何使用Python高效绘制矩形图形
    本文详细介绍了如何利用Python的Turtle库高效绘制矩形图形,适合初学者快速上手。通过具体示例代码,帮助读者理解Turtle库的基本绘图方法和技巧,同时探讨了在不同应用场景中绘制矩形的实际操作,为后续复杂图形的绘制打下坚实基础。 ... [详细]
  • 智能制造数据综合分析与应用解决方案
    在智能制造领域,生产数据通过先进的采集设备收集,并利用时序数据库或关系型数据库进行高效存储。这些数据经过处理后,通过可视化数据大屏呈现,为生产车间、生产控制中心以及管理层提供实时、精准的信息支持,助力不同应用场景下的决策优化和效率提升。 ... [详细]
  • 本文深入探讨了数据库性能优化与管理策略,通过实例分析和理论研究,详细阐述了如何有效提升数据库系统的响应速度和处理能力。文章首先介绍了数据库性能优化的基本原则和常用技术,包括索引优化、查询优化和存储管理等。接着,结合实际应用场景,讨论了如何利用容器化技术(如Docker)来部署和管理数据库,以提高系统的可扩展性和稳定性。最后,文章还提供了具体的配置示例和最佳实践,帮助读者在实际工作中更好地应用这些策略。 ... [详细]
  • 本文详细解析了如何使用 jQuery 实现一个在浏览器地址栏运行的射击游戏。通过源代码分析,展示了关键的 JavaScript 技术和实现方法,并提供了在线演示链接供读者参考。此外,还介绍了如何在 Visual Studio Code 中进行开发和调试,为开发者提供了实用的技巧和建议。 ... [详细]
  • 如何运用蒙特卡洛方法计算NPV:计算机专业毕业设计遇到难题怎么办?
    许多计算机科学专业的学生在大学期间都会遇到这样的困扰:课堂上教授的内容往往偏向理论,实际应用的知识点讲解得较为浅显和概括,导致在进行毕业设计时,如运用蒙特卡洛方法计算净现值(NPV)等复杂问题时感到无从下手。本文旨在探讨如何通过深入理解和实践蒙特卡洛模拟技术,解决这类计算难题,为学生的毕业设计提供实用指导。 ... [详细]
  • 作为140字符的开创者,Twitter看似简单却异常复杂。其简洁之处在于仅用140个字符就能实现信息的高效传播,甚至在多次全球性事件中超越传统媒体的速度。然而,为了支持2亿用户的高效使用,其背后的技术架构和系统设计则极为复杂,涉及高并发处理、数据存储和实时传输等多个技术挑战。 ... [详细]
  • 在Ubuntu系统中,由于预装了MySQL,因此无需额外安装。通过命令行登录MySQL时,可使用 `mysql -u root -p` 命令,并按提示输入密码。常见问题包括:1. 错误 1045 (28000):访问被拒绝,这通常是由于用户名或密码错误导致。为确保顺利连接,建议检查MySQL服务是否已启动,并确认用户名和密码的正确性。此外,还可以通过配置文件调整权限设置,以增强安全性。 ... [详细]
  • 如何将PHP文件上传至服务器及正确配置服务器地址 ... [详细]
  • Ceph API微服务实现RBD块设备的高效创建与安全删除
    本文旨在实现Ceph块存储中RBD块设备的高效创建与安全删除功能。开发环境为CentOS 7,使用 IntelliJ IDEA 进行开发。首先介绍了 librbd 的基本概念及其在 Ceph 中的作用,随后详细描述了项目 Gradle 配置的优化过程,确保了开发环境的稳定性和兼容性。通过这一系列步骤,我们成功实现了 RBD 块设备的快速创建与安全删除,提升了系统的整体性能和可靠性。 ... [详细]
  • 掌握PHP框架开发与应用的核心知识点:构建高效PHP框架所需的技术与能力综述
    掌握PHP框架开发与应用的核心知识点对于构建高效PHP框架至关重要。本文综述了开发PHP框架所需的关键技术和能力,包括但不限于对PHP语言的深入理解、设计模式的应用、数据库操作、安全性措施以及性能优化等方面。对于初学者而言,熟悉主流框架如Laravel、Symfony等的实际应用场景,有助于更好地理解和掌握自定义框架开发的精髓。 ... [详细]
  • 浅析PHP中$_SERVER[
    在PHP后端开发中,`$_SERVER["HTTP_REFERER"]` 是一个非常有用的超级全局变量,它可以获取用户访问当前页面之前的URL。本文将详细介绍该变量的使用方法及其在不同场景下的应用,如页面跳转跟踪、安全验证和用户行为分析等。通过实例解析,帮助开发者更好地理解和利用这一功能。 ... [详细]
  • 从用户转型为开发者:一场思维升级的旅程 | 专访 StarRocks Committer 周威
    从用户转变为开发者,不仅是一次角色的转换,更是一场深刻的思维升级之旅。本次专访中,StarRocks Committer 周威分享了他如何在这一过程中逐步提升技术能力与思维方式,为开源社区贡献自己的力量。 ... [详细]
  • 本课程详细介绍了如何使用Python Flask框架从零开始构建鱼书应用,涵盖高级编程技巧和实战项目。通过视频教学,学员将学习到Flask的高效用法,包括数据库事务处理和书籍交易模型的实现。特别感谢AI资源网提供的课程下载支持。 ... [详细]
author-avatar
想要铭记的总是轻易忘却-
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有