仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 938|回复: 19
打印 上一主题 下一主题

[学习教程] PHP网站制作之同时撑持三个MySQL+SQLite+PDO的PHP数据...

[复制链接]
再见西城 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:30:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
只要实现最基本的功能就可以了 就是可以添加留言 然后可以显示留言,然后加入管理功能     
    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;
    }
    }
熟悉HTML/CSS/JS等网页基本元素,完成阶段可自行制作完整的网页,对元素属性达到熟悉程度
兰色精灵 该用户已被删除
沙发
发表于 2015-2-4 00:06:16 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
只想知道 该用户已被删除
板凳
发表于 2015-2-9 07:54:27 | 只看该作者
我还是推荐用firefox ,配上firebug 插件调试js能省下不受时间。谷歌的浏览器最好也不少用,因为谷歌的大侠们实在是太天才啦,把一些原来的js代码加了一些特效。
灵魂腐蚀 该用户已被删除
地板
发表于 2015-2-13 05:46:30 | 只看该作者
我学习了一段时间后,我发现效果并不好(估计是我自身的问题)。因为一个人的精力总是有限的,同时学习这么多,会导致每个的学习时间都得不到保证。
活着的死人 该用户已被删除
5#
发表于 2015-2-16 09:14:12 | 只看该作者
当然这种网站的会员费就几十块钱。
金色的骷髅 该用户已被删除
6#
发表于 2015-3-1 18:17:43 | 只看该作者
这些中手常用的知识,当你把我说的这些关键字都可以熟练运用的时候,你可以选择自己
乐观 该用户已被删除
7#
发表于 2015-3-10 21:54:05 | 只看该作者
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
再现理想 该用户已被删除
8#
发表于 2015-3-17 10:53:13 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
飘灵儿 该用户已被删除
9#
发表于 2015-3-20 19:05:27 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
不帅 该用户已被删除
10#
发表于 2015-3-28 03:28:29 | 只看该作者
学习php的目的往往是为了开发动态网站,phper就业的要求也涵盖了很多。我大致总结为:精通php和mysql
简单生活 该用户已被删除
11#
发表于 2015-3-28 04:46:15 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
愤怒的大鸟 该用户已被删除
12#
发表于 2015-3-31 16:18:46 | 只看该作者
本文当是我的笔记啦,遇到的问题随时填充
admin 该用户已被删除
13#
发表于 2015-4-2 16:14:18 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
再见西城 该用户已被删除
14#
 楼主| 发表于 2015-4-16 05:00:10 | 只看该作者
兴趣是最好的老师,百度是最好的词典。
小女巫 该用户已被删除
15#
发表于 2015-4-28 18:48:18 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
小妖女 该用户已被删除
16#
发表于 2015-5-6 02:49:12 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
若天明 该用户已被删除
17#
发表于 2015-5-6 21:09:38 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
爱飞 该用户已被删除
18#
发表于 2015-6-9 21:13:02 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
飘飘悠悠 该用户已被删除
19#
发表于 2015-6-19 19:53:11 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
透明 该用户已被删除
20#
发表于 2015-6-30 15:36:47 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-6-14 04:06

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表