ホームページ php教程 php手册 MySQL+SQLite+PDOの3つのPHPデータベースクラスを同時にサポート

MySQL+SQLite+PDOの3つのPHPデータベースクラスを同時にサポート

Jun 21, 2016 am 08:51 AM
gt quot return sqlite this

PHP 学習チュートリアルの記事紹介: 3 つの MySQL+SQLite+PDO を同時にサポートする PHP データベース クラスの使用方法: // mysql connect $db = new SQL(mysql:host=localhost;database=21andy_blog;, 21andy.com_user , 21andy.com_password ); // PDO SQLite3 接続 $db = 新しい SQL(pdo:database=/21andy.com/21andy.s

3 つの MySQL+SQLite+PDO を同時にサポートする PHP データベース クラスの使用方法:

// mysql 接続

$db = 新しい SQL('mysql:host=localhost;database=21andy_blog;', '21andy.com_user', '21andy.com_password');

// PDO SQLite3 接続

$db = 新しい SQL('pdo:database=/21andy.com/21andy.sqlite3;');

// SQLite2 接続

$db = 新しい SQL('sqlite:database=/21andy.com/21andy.sqlite;');

sqldbs.class.php ファイル

/*

SQL Buddy - Web ベースの MySQL 管理

sqldbs.class.php

- SQL クラス

MITライセンス

*/

クラス SQL {

var $adapter = "";

var $method = "";

var $version = "";

var $conn = "";

var $options = "";

var $errorMessage = "";

var $db = "";

関数 SQL($connString, $user = "", $pass = "") {

list($this->adapter, $options) =explode(":", $connString, 2);

if ($this->adapter != "sqlite") {

$this->adapter = "mysql";

}

$optionsList =explode(";", $options);

foreach ($optionsList として $option) {

list($a, $b) =explode("=, $option);

$opt[$a] = $b;

}

$this->オプション = $opt;

$database = (array_key_exists("データベース", $opt)) $opt['データベース'] : "";

if ($this->adapter == "sqlite" && substr(sqlite_libversion(), 0, 1) == "3" && class_exists("PDO") && in_array("sqlite", PDO::getAvailableDrivers()) ) {

$this->method = "pdo";

試してください

{

$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";

試してください

{

$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;

}

}

関数 isConnected() {

return ($this->conn !== false);

}

関数 close() {

return $this->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() {

  $this->adapter;

を返す

  }

  function getMethod() {

  $this->メソッドを返す;

  }

  function getOptionValue($optKey) {

  if (array_key_exists($optKey, $this->options)) {

  return $this->options[$optKey];

  } else {

  false を返します;

  }

  }

  関数 selectDB($db) {

  if ($this->conn) {

  if ($this->method == "mysql") {

  $this->db = $db;

  return (mysql_select_db($db));

  } else {

  true を返します;

  }

  } else {

  false を返します;

  }

  }

  関数クエリ($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];

  }

  $queryResult を返す;

  } else if ($this->method == "mysql") {

  $queryResult = @mysql_query($queryText, $this->conn);

  if (!$queryResult) {

  $this->errorMessage = mysql_error();

  }

  $queryResult を返す;

  } else if ($this->method == "sqlite") {

  $queryResult = sqlite_query($this->conn, $queryText);

  if (!$queryResult) {

  $this->errorMessage = sqlite_error_string(sqlite_last_error($this->conn));

  }

  $queryResult を返す;

  }

  } else {

  false を返します;

  }

  }

  // この関数の使用には注意してください - pdo と一緒に使用すると、ポインタが移動します

  // 結果セットの最後まで移動すると、クエリを再実行する必要があります。あなたがいない限り

  // 実際には行数が必要なので、代わりに isResultSet() 関数を使用してください

  関数 rowCount($resultSet) {

  if (!$resultSet)

  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);

  }

  }

  }

  関数 num_rows($res) {

  return $this->rowCount($res);

  }

  関数 isResultSet($resultSet) {

  if ($this->conn) {

  if ($this->method == "pdo") {

  return ($resultSet == true);

  } else {

  return ($this->rowCount($resultSet) > 0);

  }

  }

  }

  関数 fetch($res) {

  return $this->fetchAssoc($res);

  }

  関数 fetchArray($resultSet) {

  if (!$resultSet)

  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);

  }

  }

  }

  関数 fetchAssoc($resultSet) {

  if (!$resultSet)

  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);

  }

  }

  }

  関数影響を受けるRows($resultSet) {

  if (!$resultSet)

  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);

  }

  }

  }

  関数 result($resultSet, $targetRow, $targetColumn = "") {

  if (!$resultSet)

  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);

  }

  }

  }

  関数 listDatabases() {

  if ($this->conn) {

  if ($this->adapter == "mysql") {

  return $this->query("SHOW DATABASES");

  } else if ($this->adapter == "sqlite") {

  $this->db;

を返す

  }

  }

  }

  関数 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");

  }

  }

  }

  関数 hasCharsetSupport()

  {

  if ($this->conn) {

  if ($this->adapter == "mysql" && version_compare($this->getVersion(), "4.1", ">>")) {

  true を返します;

  } else {

  false を返します;

  }

  }

  }

  関数 listCharset() {

  if ($this->conn) {

  if ($this->adapter == "mysql") {

  return $this->query("SHOW CHARACTER SET");

  } else if ($this->adapter == "sqlite") {

  "" を返します;

  }

  }

  }

  関数 listCollat​​ion() {

  if ($this->conn) {

  if ($this->adapter == "mysql") {

  return $this->query("SHOW COLLATION");

  } else if ($this->adapter == "sqlite") {

  "" を返します;

  }

  }

  }

  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);

  }

  }

  }

  関数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) {

  // キャッシュ

  if ($this->version) {

  $this->バージョンを返す;

  }

  if ($this->adapter == "mysql") {

  $verSql = mysql_get_server_info();

  $version =explode("-", $verSql);

  $this->version = $version[0];

  $this->バージョンを返す;

  } else if ($this->adapter == "sqlite") {

  $this->version = sqlite_libversion();

  $this->バージョンを返す;

  }

  }

  }

  // テーブル内の行数を返します

  関数 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"));

  $count を返します;

  } else if ($this->adapter == "sqlite") {

  $countSql = $this->query("SELECT COUNT(*) AS 'RowCount' FROM '" . $table . "'");

  $count = (int)($this->result($countSql, 0, "RowCount"));

  $count を返します;

  }

  }

  }

  // テーブルの列情報を取得します

  function descriptionTable($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 として $column) {

  $column = トリム($column);

  $columnSplit =explode(" ", $column, 2);

  $columnName = $columnSplit[0];

  $columnType = (sizeof($columnSplit) > 1) ? $columnSplit[1] : "";

  $columnList[] = array($columnName, $columnType);

  }

  $columnList を返す;

  }

  }

  }

  /*

  すべてのデータベース、テーブル、ビューの名前、行数などを JSON 文字列で返します

  */

  関数 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'] . '"';

  // その他の興味深い列: TABLE_TYPE、ENGINE、TABLE_COLUMN など

  $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 .= ',"アイテム": [';

  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 .= ',"アイテム": [';

  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 .= ',"アイテム": [';

  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 .= '}';

  }

  }

  $output を返す;

  }

  関数 error() {

  $this->errorMessage;

を返す

  }

  }



このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Huawei GT3 ProとGT4の違いは何ですか? Huawei GT3 ProとGT4の違いは何ですか? Dec 29, 2023 pm 02:27 PM

多くのユーザーはスマートウォッチを選ぶときにファーウェイブランドを選択しますが、その中でもファーウェイ GT3pro と GT4 は非常に人気のある選択肢であり、多くのユーザーはファーウェイ GT3pro と GT4 の違いに興味を持っています。 Huawei GT3pro と GT4 の違いは何ですか? 1. 外観 GT4: 46mm と 41mm、材質はガラスミラー + ステンレススチールボディ + 高解像度ファイバーバックシェルです。 GT3pro: 46.6mm および 42.9mm、材質はサファイアガラス + チタンボディ/セラミックボディ + セラミックバックシェルです。 2. 健全な GT4: 最新の Huawei Truseen5.5+ アルゴリズムを使用すると、結果はより正確になります。 GT3pro: ECG 心電図と血管と安全性を追加

PHP と SQLite を使用してユーザー ログイン システムを作成する方法 PHP と SQLite を使用してユーザー ログイン システムを作成する方法 Jul 28, 2023 pm 09:27 PM

PHP と SQLite を使用してユーザー ログイン システムを作成する方法 今日のインターネット時代では、ユーザー ログイン システムは多くの Web サイトやアプリケーションの基本機能の 1 つです。この記事では、PHP と SQLite を使用して、シンプルで強力なユーザー ログイン システムを作成する方法を紹介します。 SQLite は組み込みデータベース エンジンであり、構成不要のサーバー側データベース エンジンです。 PHP は、SQLite と組み合わせて使用​​して、柔軟で効率的なユーザー ログイン システムを作成できる人気のあるサーバー側スクリプト言語です。による

C言語のreturnの使い方を詳しく解説 C言語のreturnの使い方を詳しく解説 Oct 07, 2023 am 10:58 AM

C 言語における return の使い方は、 1. 戻り値の型が void の関数については、return 文を使用して関数の実行を早期に終了することができます; 2. 戻り値の型が void ではない関数については、 return ステートメントは、関数の実行を終了するためのものです。結果は呼び出し元に返されます。 3. 関数の実行を早期に終了します。関数内で return ステートメントを使用して、関数の実行を早期に終了することもできます。関数が値を返さない場合。

PHP と SQLite を使用したユーザー権限とアクセス制御の実装 PHP と SQLite を使用したユーザー権限とアクセス制御の実装 Jul 29, 2023 pm 02:33 PM

PHP と SQLite を使用したユーザー権限とアクセス制御の実装 最新の Web アプリケーションでは、ユーザー権限とアクセス制御は非常に重要な部分です。適切な権限管理を行うと、許可されたユーザーのみが特定のページや機能にアクセスできるようになります。この記事では、PHP と SQLite を使用して基本的なユーザー権限とアクセス制御を実装する方法を学びます。まず、ユーザーとその権限に関する情報を保存する SQLite データベースを作成する必要があります。以下は簡単なユーザーテーブルと権限テーブルの構造です。

PHP と SQLite: データの圧縮と暗号化を行う方法 PHP と SQLite: データの圧縮と暗号化を行う方法 Jul 29, 2023 am 08:36 AM

PHP と SQLite: データを圧縮および暗号化する方法 多くの Web アプリケーションでは、データのセキュリティとストレージ領域の使用率が非常に重要な考慮事項です。 PHP と SQLite は非常に広く使用されている 2 つのツールであり、この記事ではこれらをデータ圧縮と暗号化に使用する方法を紹介します。 SQLite は、別個のサーバー プロセスを持たず、アプリケーションと直接対話する軽量の組み込みデータベース エンジンです。 PHP は、動的スクリプトを構築するために広く使用されている人気のあるサーバー側スクリプト言語です。

PHP と SQLite を使用したデータのグラフ化と視覚化 PHP と SQLite を使用したデータのグラフ化と視覚化 Jul 28, 2023 pm 01:01 PM

PHP と SQLite を使用したデータ チャートと視覚化の実装の概要: ビッグ データ時代の到来により、データ チャートと視覚化はデータを表示および分析するための重要な方法になりました。この記事では、PHPとSQLiteを使ってデータチャートや可視化機能を実装する方法を紹介します。 SQLite データベースからデータを読み取り、共通のデータ チャート ライブラリを使用してデータを表示する方法を例として挙げます。準備: まず、PHP および SQLite データベースがインストールされていることを確認する必要があります。インストールされていない場合は、

シンプルなブログを作成する: PHP と SQLite を使用する シンプルなブログを作成する: PHP と SQLite を使用する Jun 21, 2023 pm 01:23 PM

インターネットの発展に伴い、ブログはますます多くの人々が自分の生活、知識、アイデアを共有するプラットフォームになりました。自分のブログも作成したい場合は、この記事で PHP と SQLite を使用して簡単なブログを作成する方法を紹介します。ニーズを決定する ブログを作成し始める前に、実現したい機能を決定する必要があります。例: ブログ投稿の作成 ブログ投稿の編集 ブログ投稿の削除 ブログ投稿のリストの表示 ブログ投稿の詳細の表示 ユーザー認証と権限制御 PHP と SQLite のインストール PHP と S をインストールする必要があります

PHP と SQLite: 長時間の接続、切断と再接続に対処する方法 PHP と SQLite: 長時間の接続、切断と再接続に対処する方法 Jul 29, 2023 am 09:05 AM

PHP と SQLite: 長時間の接続、切断と再接続に対処する方法 はじめに: Web 開発では、PHP と SQLite はよく使用される 2 つのテクノロジです。ただし、長時間の接続や切断と再接続は、PHP や SQLite を使用するときによく発生する問題の一部です。この記事では、PHP での長時間の接続、切断と再接続の問題に対処する方法を紹介し、開発者がこれらの問題をよりよく理解して解決するのに役立ついくつかのサンプル コードを提供します。 1. 永続的な接続の問題 PHP を使用して SQLite データベースに接続すると、接続が長くなる (永続的)

See all articles