PHP : mysqli を使用した単純な問合せアプリケーション

ダウンロード

▼ デモページ

データベースの接続に必要な値は、include_path に設定されたディレクトリに config.php を作成してセットします

▼ config.php
<?php
$host = "MySQLサーバ";
$user = "ユーザ";
$pass = "パスワード";
$db = "データベース";
?>
control.php ( エントリポイント )
<?php
# ***************************
# ソースベースの取り込み
# ***************************
require_once('settings.php');
require_once('model.php');
require_once('config.php');

# ***************************
# MySQL 接続
# ***************************
$con = @new mysqli($host, $user, $pass, $db);
$con->set_charset("utf8");

# ***************************
# MySQL 用特殊文字エスケープ
# ***************************
foreach( $_REQUEST as $key => $value ) {
	$_REQUEST["db"][$key] = $con->real_escape_string($value);
}

# ***************************
# テーブル表示
# ***************************
build_table($con);

# ***************************
# MySQL 接続解除
# ***************************
$con->close();

# ***************************
# 画面定義
# ***************************
require_once('view.php');
?>



view.php ( 画面定義 )

<?php
# **************************************
# js キャッシュ用
# **************************************
$tm = mktime();

# **************************************
# 画面定義
# ( Ruby や Python に合わせた画面形式 )
# **************************************
$out_client = <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width initial-scale=1.0 minimum-scale=1.0 maximum-scale=1.0 user-scalable=no" name="viewport">
<title>MySQL 問合せ</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css" />

<script src="entry.js?{$tm}"></script>

<style>
body {
	margin: 0;
}

#main {
	padding: 0px 16px 3px 16px;
}

</style>
</head>
<body>
<div class="alert alert-dark">
	簡易タイトル
</div>
<div id="main" class="table-responsive">

	<table class="table table-hover">
	{$GLOBALS['table']}
	</table>

</div>
</body>
</html>
HTML;

print $out_client;


?>


model.php ( HTML 作成部分 )
<?php
# ***************************
# モデル用関数を定義する
# ***************************

# ***************************
# テーブル表示
# ***************************
function build_table($con) {

	// デバッグ用
	file_put_contents("debug.log", "build_table:開始\n" , FILE_APPEND );
	file_put_contents("debug.log", print_r( $con ,true ) , FILE_APPEND );

	// TR 内の HTML 文字列
	$lines = "";
	$sql= <<<SQL
		select 
			社員コード,
			氏名,
			フリガナ,
			所属,
			性別,
			作成日,
			更新日,
			給与,
			手当,
			管理者,
			DATE_FORMAT(生年月日,'%Y/%m/%d') as 生年月日
		from 社員マスタ
		where 氏名 like '%{$_REQUEST["db"]["nm"]}%'
SQL;

	$rs = $con->query($sql);	// エラー処理省略(本当は必要)
	//  デバッグ用
	file_put_contents("debug.log", print_r( $rs ,true ) , FILE_APPEND );

	// 列情報を取得( タイトル用 )
	$fields_data = $rs->fetch_fields();
	foreach( $fields_data as $field ){
		$lines .= "<th>{$field->name}</th>";
	}

	// 列データを取得
	while( $row = $rs->fetch_array(MYSQLI_BOTH) ) {
	
		$cells  = "";
		for( $i = 0; $i < $rs->field_count; $i++ ) {
			$cells .= "<td>{$row[$i]}</td>";
		}
	
		$lines .= "<tr>{$cells}</tr>" . "\n";
	}

	// 埋め込み用グローバル変数へセット	
	$GLOBALS['table'] = $lines;

	//  デバッグ用
	file_put_contents("debug.log", "build_table:終了\n" , FILE_APPEND );

}


?>


settings.php ( 共通設定 )
<?php
// ***************************
// 共通処理( UTF8N で保存 )
// ***************************
error_reporting( E_ALL & ~E_NOTICE & ~E_STRICT );
ini_set('display_errors', '1');
ini_set('date.timezone', 'Asia/Tokyo');
ini_set('default_charset', 'utf-8');
session_cache_limiter('nocache');
session_start();
header( "Content-Type: text/html; charset=utf-8" );


foreach( $_REQUEST as $key => $value ) {

# ここで $_REQUEST 内の文字列のセキュリティ上の処理

}

# デバッグログの初期化
file_put_contents("debug.log", "開始\n" );

# メッセージ
$check_message = "";

# クライアントコントロール
$pass = "1";
?>