萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> 同時支持三個MySQL+SQLite+PDO的PHP數據庫類

同時支持三個MySQL+SQLite+PDO的PHP數據庫類

class="area"> 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 $resultSe

copyright © 萬盛學電腦網 all rights reserved