このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン 次のリビジョン 両方とも次のリビジョン | ||
mariadb:10.4:mysqli [2019/10/16 21:08] y2sunlight [データの更新] |
mariadb:10.4:mysqli [2020/11/20 08:05] y2sunlight [サンプルプログラムについて] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > **本編は投稿中です。しばらくお待ち下さい。** | + | ====== MariaDB10.4 PHPサンプルプログラム ====== |
+ | phpMyAdmin 4.9.0.1 (MariaDB 10.4.6) | ||
- | ---- | ||
- | |||
- | ====== MariaDB10.4 PHPサンプルプログラム ====== | ||
--- // | --- // | ||
- | |||
[[mariadb: | [[mariadb: | ||
関連記事 | 関連記事 | ||
- | * [[xampp: | ||
* [[mariadb: | * [[mariadb: | ||
* MariaDB10.4 PHPサンプルプログラム | * MariaDB10.4 PHPサンプルプログラム | ||
行 17: | 行 13: | ||
* [[mariadb: | * [[mariadb: | ||
- | ---- | + | 以下「MySQL」は「MariaDB」に読み替えて下さい。 |
- | ここでは、MySQLの動作を理解するために、サンプルプログラム [[# | + | ----- |
+ | |||
+ | ===== サンプルプログラムについて ===== | ||
+ | |||
+ | 本章では、MySQLの動作を理解するために、サンプルプログラム [[# | ||
サンプルプログラムでは MySQLi を使用します。MySQLiは、MySQL(またはMariaDB)データベースとの接続を行うPHP拡張モジュールで、前章で説明した phpMyAdmin でも使用されています。 | サンプルプログラムでは MySQLi を使用します。MySQLiは、MySQL(またはMariaDB)データベースとの接続を行うPHP拡張モジュールで、前章で説明した phpMyAdmin でも使用されています。 | ||
+ | |||
+ | サンプルプログラムでは以下のデータベースを使用します。作り方は、本編の「[[mariadb: | ||
+ | |||
+ | ^ ホスト名 | localhost | | ||
+ | ^ データベース名 | sunlight_db | | ||
+ | ^ 照合順序 | utf8_bin | | ||
+ | ^ ユーザ名 | sunlight | | ||
+ | ^ パスワード | sunlight | | ||
参考リンク | 参考リンク | ||
* https:// | * https:// | ||
+ | |||
+ | 本章のサンプルプログラムの最新版はGitHubで公開しています | ||
+ | * https:// | ||
+ | \\ | ||
===== mysqli クラスの使い方 ===== | ===== mysqli クラスの使い方 ===== | ||
行 75: | 行 87: | ||
</ | </ | ||
- | ===== データベースとの接続 | + | \\ |
+ | |||
+ | ===== mysqli クラスの使用例 ===== | ||
+ | |||
+ | ==== データベースとの接続 ==== | ||
- | 例 | ||
<file php sample1.php> | <file php sample1.php> | ||
<?php | <?php | ||
行 101: | 行 116: | ||
</ | </ | ||
- | ===== テーブルの作成 | + | ==== テーブルの作成 ==== |
- | 例 | ||
<file php sample2.php> | <file php sample2.php> | ||
<?php | <?php | ||
行 143: | 行 157: | ||
</ | </ | ||
- | ===== データの更新 | + | ==== データの更新 ==== |
- | 例 | ||
<file php sample3.php> | <file php sample3.php> | ||
<?php | <?php | ||
行 200: | 行 213: | ||
</ | </ | ||
- | ===== データの検索 | + | ==== データの検索 ==== |
- | 例 | ||
<file php sample4.php> | <file php sample4.php> | ||
<?php | <?php | ||
行 247: | 行 259: | ||
else | else | ||
{ | { | ||
- | | + | |
+ | var_dump($result); | ||
} | } | ||
return $ret; | return $ret; | ||
行 253: | 行 266: | ||
</ | </ | ||
+ | \\ | ||
===== サンプルプログラム ===== | ===== サンプルプログラム ===== | ||
行 259: | 行 273: | ||
* プログラムはSQLスクリプトファイル(テキストファイル)を読み込みます。 | * プログラムはSQLスクリプトファイル(テキストファイル)を読み込みます。 | ||
- | * SQLスクリプトファイルにはSQL文、EVAL文、コメントを含みます。\\ | + | * ファイルにはSQL文、EVAL文、コメントを含み、文はセミコロン('' |
+ | * 行コメント( '' | ||
+ | * 改行を出力したい場合は ''#;'' | ||
* SQL文がSELECT文( またはSHOW文 )の場合は検索結果を表示し、非SELECT文の場合はSQLの実行のみ行います。 | * SQL文がSELECT文( またはSHOW文 )の場合は検索結果を表示し、非SELECT文の場合はSQLの実行のみ行います。 | ||
- | * EVAL文はPHPのeval関数で実行します。但し複文の実行はできません。また、クォート処理をしていないので、EVAL echo ' | + | * EVAL文はPHPのeval関数で実行します。但し複文の実行はできません。例えば:'' |
+ | * EVAL文はクォート処理をしていないので、'' | ||
* 検索結果及び実行結果はHTMLで出力します。 | * 検索結果及び実行結果はHTMLで出力します。 | ||
- | * SQLスクリプトファイルはURLのクエリ文字列で指定します。\\ 例えば:< | + | * SQLスクリプトファイルはURLのクエリ文字列で指定します。\\ 例えば:'' |
+ | * クライアント文字セットをUTF8に設定することができます(sオプション)。 | ||
=== サンプルプログラムを使った例 === | === サンプルプログラムを使った例 === | ||
- | ブラウザからサンプルプログラム(sqlfile.cgi)を実行します。 | + | ブラウザからサンプルプログラム(sqlfile.php)を実行します。 |
< | < | ||
- | http:// | + | http:// |
</ | </ | ||
<file sql sample.sql> | <file sql sample.sql> | ||
- | # テーブル作成 | + | -- テーブル作成 |
DROP TABLE IF EXISTS syain; | DROP TABLE IF EXISTS syain; | ||
CREATE TABLE syain ( | CREATE TABLE syain ( | ||
行 282: | 行 300: | ||
); | ); | ||
- | # テーブルにデータを挿入 | + | -- テーブルにデータを挿入 |
INSERT INTO syain VALUES(1,' | INSERT INTO syain VALUES(1,' | ||
INSERT INTO syain VALUES(2,' | INSERT INTO syain VALUES(2,' | ||
INSERT INTO syain VALUES(3,' | INSERT INTO syain VALUES(3,' | ||
- | # テーブルの検索 | + | -- テーブルの検索 |
SELECT * FROM syain; | SELECT * FROM syain; | ||
</ | </ | ||
実行結果 | 実行結果 | ||
- | TODO:画像を貼る | + | [{{:mariadb: |
- | === サンプルプログラム( sqlfile.cgi )のソース === | + | // |
+ | |||
+ | === サンプルプログラム( sqlfile.php )のソース === | ||
<file php sqlfile.php> | <file php sqlfile.php> | ||
+ | <?php | ||
+ | /** | ||
+ | * Configuration | ||
+ | */ | ||
+ | // データベース接続先 | ||
+ | define(" | ||
+ | define(" | ||
+ | define(" | ||
+ | define(" | ||
+ | define(" | ||
+ | |||
+ | // SQLファイルの保存先 | ||
+ | define(" | ||
+ | |||
+ | /** | ||
+ | * Main | ||
+ | */ | ||
+ | // SQスクリプト取得 | ||
+ | if (isset($_REQUEST[' | ||
+ | { | ||
+ | $sql_text = ReadScriptFile(SQL_PATH . "/ | ||
+ | $sql_array = GetSqlText($sql_text); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | die(' | ||
+ | } | ||
+ | |||
+ | // データベースへの接続 | ||
+ | $mysqli = @new mysqli( HOST, USER_NAME, PASSWORD, DATABASE_NAME, | ||
+ | if( $mysqli-> | ||
+ | { | ||
+ | die($mysqli-> | ||
+ | } | ||
+ | |||
+ | // 文字セットの指定 | ||
+ | if (isset($_REQUEST[' | ||
+ | { | ||
+ | $mysqli-> | ||
+ | } | ||
+ | |||
+ | // レスポンス処理 | ||
+ | HTML_Begin(); | ||
+ | DoSqlScript( $mysqli, $sql_array ); | ||
+ | HTML_End(); | ||
+ | |||
+ | // データベースの切断 | ||
+ | $mysqli-> | ||
+ | |||
+ | /** | ||
+ | * SQLスクリプトの実行 | ||
+ | * @param mysqli $mysqli MySQLiオブジェクト | ||
+ | * @param string[] $sql_text SQL文の配列 | ||
+ | */ | ||
+ | function DoSqlScript( mysqli $mysqli, array $sql_text ) | ||
+ | { | ||
+ | if (empty($sql_text)) return; | ||
+ | |||
+ | $sqltime = 0; | ||
+ | foreach( $sql_text as $sql ) | ||
+ | { | ||
+ | if ( !$sql || $sql[0]=='#' | ||
+ | |||
+ | // SQL文表示 | ||
+ | print HTML_Escape(strlen($sql)< | ||
+ | print "< | ||
+ | ob_flush(); flush(); | ||
+ | |||
+ | if ( !$sql ) continue; # 空行 | ||
+ | |||
+ | // 特別なEVAL文の実行 | ||
+ | if ( preg_match( "/ | ||
+ | { | ||
+ | eval(" | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | // SELECT文と非SELECT文で処理を分ける | ||
+ | $time1 = microtime_as_float(); | ||
+ | if (preg_match("/ | ||
+ | { | ||
+ | if (!DoSelect($mysqli, | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | $res = $mysqli-> | ||
+ | if($res===false) | ||
+ | { | ||
+ | SqlError(" | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | $time2 = microtime_as_float(); | ||
+ | $sqltime += ($time2-$time1); | ||
+ | |||
+ | ob_flush(); flush(); | ||
+ | } | ||
+ | print "exec time: " | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * SELECT文の実行 | ||
+ | * @param mysqli $mysqli MySQLiオブジェクト | ||
+ | * @param string $sql SQL文 | ||
+ | * @return boolean 成功でTrueを返す | ||
+ | */ | ||
+ | function DoSelect( mysqli $mysqli, string $sql ) | ||
+ | { | ||
+ | // 検索実行 | ||
+ | $res = $mysqli-> | ||
+ | if($res===false) | ||
+ | { | ||
+ | SqlError(" | ||
+ | return false; | ||
+ | } | ||
+ | |||
+ | // カラム名の表示 | ||
+ | if ($rows = $res-> | ||
+ | { | ||
+ | print "< | ||
+ | print "< | ||
+ | foreach( $rows[0] as $key => $value ) | ||
+ | { | ||
+ | print "< | ||
+ | } | ||
+ | print "</ | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | print "No results found.< | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | // 行の表示 | ||
+ | foreach( $rows as $row ) | ||
+ | { | ||
+ | print "< | ||
+ | foreach( $row as $value ) | ||
+ | { | ||
+ | $value = HTML_Escape($value); | ||
+ | print "< | ||
+ | } | ||
+ | print "</ | ||
+ | } | ||
+ | |||
+ | print "</ | ||
+ | return true; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * SQLテキストの取得 | ||
+ | * @param string $text SQLテキスト | ||
+ | * @return string[] SQL文の配列 | ||
+ | */ | ||
+ | function GetSqlText( string $text ) | ||
+ | { | ||
+ | $text = str_replace([" | ||
+ | |||
+ | // Remove comment | ||
+ | $text = preg_replace("/ | ||
+ | $text = preg_replace("/ | ||
+ | |||
+ | // Split SQL text | ||
+ | $sql = preg_split("/ | ||
+ | array_walk($sql, | ||
+ | $item = trim($item); | ||
+ | }); | ||
+ | $sql = array_filter($sql, | ||
+ | return !empty(trim($val)); | ||
+ | }); | ||
+ | return $sql; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * SQLスクリプトファイルの読み込み | ||
+ | * @param string $sql_file ファイル名 | ||
+ | * @return string SQLテキスト | ||
+ | */ | ||
+ | function ReadScriptFile( string $sql_file ) | ||
+ | { | ||
+ | if (!preg_match("/ | ||
+ | { | ||
+ | SqlError(" | ||
+ | } | ||
+ | |||
+ | if(($text=@file_get_contents($sql_file))===false) | ||
+ | { | ||
+ | SqlError(" | ||
+ | } | ||
+ | return $text; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | | ||
+ | * @return float | ||
+ | */ | ||
+ | function microtime_as_float() | ||
+ | { | ||
+ | list($usec, $sec) = explode(' | ||
+ | return ((float)$sec + (float)$usec); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * HTML エスケープ | ||
+ | * @param string $str | ||
+ | * @return string | ||
+ | */ | ||
+ | function HTML_Escape( string $str ) | ||
+ | { | ||
+ | return htmlentities( $str, ENT_QUOTES, " | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * HTML エスケープ(FORM用) | ||
+ | * @param string $str | ||
+ | * @return string | ||
+ | */ | ||
+ | function FORM_Escape( string $str ) | ||
+ | { | ||
+ | return htmlspecialchars( $str, ENT_QUOTES, " | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * HTMLの開始処理 | ||
+ | */ | ||
+ | function HTML_Begin() | ||
+ | { | ||
+ | header(' | ||
+ | print <<< | ||
+ | < | ||
+ | < | ||
+ | <style type=" | ||
+ | body {font:10pt monospace;} | ||
+ | table {font:10pt monospace; | ||
+ | th,td {border: | ||
+ | th {background-color:# | ||
+ | </ | ||
+ | </ | ||
+ | < | ||
+ | EOF; | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * HTMLの終了処理 | ||
+ | */ | ||
+ | function HTML_End(){ | ||
+ | print "</ | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * エラーメッセージ表示 | ||
+ | * @param string $title タイトル | ||
+ | * @param string $msg メッセージ | ||
+ | */ | ||
+ | function SqlError($title, | ||
+ | { | ||
+ | print "< | ||
+ | } | ||
</ | </ | ||
+ | |||
+ | \\ | ||