SQLの窓

2019年02月03日


PHP で MySQLをテストする為のソースコード


※ $_GET['text'] で入力された SQL が引き渡されます。
※ GET コマンドなので、内容はアドレスバーで確認する事ができます
※ Form を使わずにアドレスバーから直接 SQL を実行できます。
( IE11 は不可 )

列(セル)の HTML の出力用に関数を作成しています。内部は『ヒアドキュメント』を利用した比較的可読性の高い方法を用いて文字列を作成しています。

ヒアドキュメント構文では、最後の識別子をインデントしてはいけないので関数内でHTML の階層を単純化するほうがメンテナンス性が高くなると思います。

QueryString に text が無い場合と text に有効な文字が全く無い場合は  show variables でシステム変数の一覧を表示します
if ( !isset( $_GET['text'] ) || trim($_GET['text']) == "" ) {
	$_GET['text'] = "show variables";
}
MySQL 改良版拡張モジュール
<?php
session_cache_limiter('nocache');
session_start();

header( "Content-Type: text/html; charset=utf-8" );

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SQL実行結果</title> 
<style>
table {
	border: solid 1px #000;
	border-collapse: collapse;
}

th,td {
	border: solid 1px #000;
	padding: 5px;
}
</style>
</head>
<body style='white-space:pre;'>
<?php
if ( !isset( $_GET['text'] ) || trim($_GET['text']) == "" ) {
	$_GET['text'] = "show variables";
}

print $_GET['text'] . "\n";

$server = 'localhost';
$dbname = 'lightbox';
$user = 'root';
$password = 'パスワード';

// ***************************
// 接続
// ***************************
$mysqli = @ new mysqli($server, $user, $password, $dbname);
if ($mysqli->connect_error) {
	print "接続エラーです : ({$mysqli->connect_errno}) ({$mysqli->connect_error})";
	exit();
}

// ***************************
// クライアントの文字セット
// ***************************
$mysqli->set_charset("utf8"); 

// ***************************
// クエリ
// ***************************
$result = $mysqli->query($_GET['text']);
if ( !$result ) {
	print "\n";
	print "<span style='color:#f00'>error : " . $mysqli->error . "</span>";
	exit();
}

// ***************************
// 列数
// ***************************
$nfield = $result->field_count;
if ( $nfield ) {
	$ncount = 0;
	print "<table>\n";

	// 行番号用タイトル
	print "\t<th></th>";

	// 列のタイトルを作成
	$field = $result->fetch_fields( );
	for( $i = 0; $i < $nfield; $i++ ) {

		print_cell_html( "th", $field[$i]->name );

	}

	print "\n";

	// ***************************
	// 行データ
	// ***************************
	while ($row = $result->fetch_row()) {

		print "<tr>\n";
		// 行番号
		print "\t<td>" . ($ncount + 1) . "</td>";

		for( $i = 0; $i < $nfield; $i++ ) {

			print_cell_html( "td", $row[$i] );

		}
		print "\n</tr>\n";

		$ncount++;
	}

	print "</table>";
}

// ***************************
// 接続解除
// ***************************
$mysqli->close();


// ***************************
// セルの HTML 出力
// ***************************
function print_cell_html( $html, $data ) {

print <<<CELL_HTML
<{$html}>{$data}</{$html}>
CELL_HTML;

}

?>

</body>
</html>


PHP 5.5.0 で非推奨になり、PHP 7.0.0 で削除
<?php
session_cache_limiter('nocache');
session_start();

header( "Content-Type: text/html; charset=utf-8" );

?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SQL実行結果</title> 
</head>
<body style='white-space:pre;'>
<?php
print $_GET['text'] . "\n";

$server = 'localhost';
$dbname = 'lightbox';
$user = 'root';
$password = 'パスワード';

// ***************************
// 接続
// ***************************
$connect = @mysql_connect( $server, $user, $password );
if ( !$connect ) {
	print "接続エラーです";
	exit();
}

// ***************************
// DB選択
// ***************************
mysql_select_db( $dbname, $connect );
mysql_set_charset("utf8", $connect); 

// ***************************
// クエリ
// ***************************
$result = mysql_query($_GET['text'], $connect);
if ( !$result ) {
	print "\n";
	print "<span style='color:#f00'>" . mysql_error() . "</span>";
}

// ***************************
// 列数
// ***************************
$nfield = @mysql_num_fields( $result );
if ( $nfield ) {
	$ncount = 0;
	print "<table style='border:solid 1px #000;border-collapse:collapse;'>\n";

	print "\t<th style='border:solid 1px #000;padding:5px;'></th>\n";
	for( $i = 0; $i < $nfield; $i++ ) {
		$field = mysql_fetch_field( $result, $i );

		print "\t<th style='border:solid 1px #000;padding:5px;'>{$field->name}</th>\n";
	}

	while ($row = mysql_fetch_row($result)) {
		print "<tr>\n";
		print "\t<td style='border:solid 1px #000;padding:5px;'>" . ($ncount + 1) . "</td>\n";
		for( $i = 0; $i < $nfield; $i++ ) {
			print "\t<td style='border:solid 1px #000;padding:5px;'>{$row[$i]}</td>\n";
		}
		print "</tr>\n";
		$ncount++;
	}
	print "</table>";
}

// ***************************
// 接続解除
// ***************************
mysql_close($connect);
?>

</body>
</html>

※ これらのコードは、開発上のテストを目的としているのでセキュリティ上の考慮はされていません。



【PHP + データベースの最新記事】
posted by lightbox at 2019-02-03 18:37 | PHP + データベース | このブログの読者になる | 更新情報をチェックする
container 終わり



フリーフォントで簡単ロゴ作成
フリーフォントでボタン素材作成
フリーフォントで吹き出し画像作成
フリーフォントではんこ画像作成
ほぼ自由に利用できるフリーフォント
フリーフォントの書体見本とサンプル
画像を大きく見る為のウインドウを開くボタンの作成

CSS ドロップシャドウの参考デモ
イラストAC
ぱくたそ
写真素材 足成
フリーフォント一覧
utf8 文字ツール
右サイド 終わり
base 終わり