同时支持三个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;

  }

  }

时间: 2024-08-01 11:30:35

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

数据库操作类mysql/mysqli/pdo

PDO,MySQL,MYSQLI的各自不同介绍,PDO,MYSQL,MYSQLI 性能哪个比较好 普通的mysql连接肯定是会被抛弃的 因为每次都要防止sql注入的问题 而且相对来说比较慢 首先, mysqli 连接是永久连接,而mysql是非永久连接 .什么意思呢? mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以很大程度的减轻服务器端压力. mysqli是在普通mysql的基础上做的一次优化说实话很成功 预处理方式完全解决了sql注入的

mysql,mysqli,PDO的各自不同介绍_php文摘

普通的mysql连接肯定是会被抛弃的 因为每次都要防止sql注入的问题 而且相对来说比较慢 mysqli是在普通mysql的基础上做的一次优化 说实话 很成功 预处理方式完全解决了sql注入的问题 但是唯一的不足点 就是只支持mysql数据库 当然 如果你要是不操作其他的数据库或者 当然这无疑是最好的选择 PDO则是最新出来的一种 连接方式 兼容大部分数据库 也解决了sql注入 但是也有缺点 它只支持php5以上的版本 不过听说在未来的php6中 只支持这种连接 PDO统一所有数据库抽象层对象接

基于全志A20 android4.2平台如何支持三个SD卡

基于全志A20 android4.2平台如何支持三个SD卡            做过android平台的同仁大多都知道android原生态只支持了一个sd卡,默认的挂载点也就是/mnt/sdcard,所以在应用中使用getExternalStorageDirectory()得到的都是/mnt/sdcard,通常会symlink 到/sdcard目录.做过全志平台的童鞋也知道全志android SDK支持2个sd卡,通常是一个内置的,一个外置的,内置的一般是从nand上或者emmc上的用户数据区

在Debian Wheezy上支持PHP5和MySQL的Apache2的安装说明

LAMP 是Linux, http://www.aliyun.com/zixun/aggregation/14417.html">Apache, MySQL, PHP的简写.该教程演示了如何在 Debian Wheezy 服务器上安装支持 PHP5 和 MySQL 的 Apache2 服务器. 1.初步说明 在本教程中,我们使用的主机名是 server1.example.com,IP地址是 192.168.0.100.这些设置可能与你的有所不同,所以你需在安装完成后改回自己正确的IP地址.

免费空间,支持php asp mysql 数据库

免费赞助空间(asp050型) 网站空间 50M  数据库空间 支持MYSQL/MSSQL空间,需要另外购买 Access数据库免费(跟web空间共享数据库大小) 捆绑专业邮局 √  售价 0元/年     机房环境:Win2003+IIS6.0+ASP+ASP.NET+PHP+Shtml+不能放游戏站点+禁止下载 WEB空间大小:200M  可绑域名:5个 支持ACCESS数据库(与虚拟主机共享空间) IIS并发数:60  CPU使用率:2%  网络流量:500M/月 文件大小:不限制 文件类

中方要求有关国家停止资助支持三股势力

新华网北京7月14日电 外交部发言人秦刚14日在例行记者会上表示,中方强烈要求有关国家停止向包括"东突"势力在内的"三股势力"提供任何形式的资助和支持. 秦刚说,大量事实证明,境内外"三股势力"近年来不断从事危害中国国家安全的活动,有关国家为他们提供包庇.纵容和资助. 美国国务院发言人凯利13日在新闻发布会上回答记者提问时说,以热比娅为首的"世界维吾尔代表大会"的确从美国全国维护民主捐赠基金会获取资金援助,而美国全国维护民主捐

支持CGI/PHP/MYSQL的免费空间

美国--100MB高速PHP+CGI免费空间 支持PHP.CGI.Perl.Htaccess.MySQL 活动地址:http://auction1.taobao.com/auction/item_detail-0db1-3186d9d967974843d10d950df0f10391.jhtml DotLayer提供1G免费php+cgi空间申请 该免费PHP+CGI空间大小为:1GB : 月流量限制为:20GB : 支持FTP/web方式上传与管理文件 : 支持PHP5.CGI.Perl.Py

mysql_connect() 不支持 请检查 mysql 模块是否正确加载

把以下代码保存为phpinfo.php: <?php phpinfo(); ?> mysql ------- MySQL Support  enabled Active Persistent Links 1 Active Links  1 Client API version  5.0.37 ------- 如果看不到, 而且你确定数据库已经安装了, 那么可能需要在服务器中加载libmysql.dll: 打开httpd.conf (这个是Apache的配置文件), 加入: LoadFile &

php中PDO方式实现数据库的增删改查

  PDO是mysql数据库操作的一个公用类了,我们不需要进行自定类就可以直接使用pdo来操作数据库了,但是在php默认配置中pdo是未开启所以我们必须先在php.ini中开启它才可以使用. 需要开启php的pdo支持,php5.1以上版本支持 实现数据库连接单例化,有三要素 静态变量.静态实例化方法.私有构造函数 DPDO.php ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30