日期:2011-10-07  浏览次数:20444 次

 

  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->di