まず、作ってみるのが以下のサンプルコード
サクッと書いて一回サーバーに挙げて見るの。
サーバーの情報がある程度分かるから、作り込む際に楽なの。
あ、書いた毎に、一回実行してみて結果を確認するの。
間違いがあれば、すぐに気がつくはず。
(*サンプルには、行番号が入ってたりする事があるけど、実際には不要。)
info.php
1:<?
2: phpinfo();
3:?>
4:
次に1つ選択しなきゃいけない事があるの。
それは、データベースを使うかどうか?
今回は、使うと言う事にしますか。
config.php
01:<?php
02:if(!defined("_MAIN_")) {
03: header("HTTP/1.1 404 Not Found");
04: die("Not Found");
05:}
06:
07:class Config {
08: var $server = "server name";
09: var $port = "port number";
10: var $user = "user";
11: var $pwd = "password";
12:}
13:?>
14:
index.php
1:<?php
2: define("_MAIN_", "1");
3: include './config.php';
4:?>
5:SUCCESS
6:
コードをデータベースに接続する様に変更するの。
今回は、MySQLを選択する事にしたの。
あっ、その時に、donfig.phpの内容も正しいものに変更しないとダメだったり。
serverは、サーバー名、userは、ユーザ名、pwdは、パスワードを入れるの。
index.php
01:<?php
02: define("_MAIN_", "1");
03: include './config.php';
04: $config = new Config();
05: $mysql = mysql_connect($config->server, $config->user, $config->pwd);
06: if(!$mysql){
07: die("Not Found");
08: }
09: echo "SUCCESS";
10: mysql_close($mysql);
11:?>
12:
テーブルの一覧を取得するコードを入れるの。
どんなテーブルがあるか、一発で分かると便利だからね。
01:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
02:<html>
03: <head>
04: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
05: <title></title>
06: </head>
07: <body>
08: <?php
09: define("_MAIN_", "1");
10: include './config.php';
11: $config = new Config();
12: $mysql = mysql_connect($config->server, $config->user, $config->pwd);
13: if (!$mysql) {
14: die("Failed Connect");
15: }
16:
17: $result = mysql_query("SHOW TABLES FROM " . $config->user, $mysql);
18: if (!$result) {
19: echo "SHOW TABLES FROM " . $config->user;
20: echo mysql_error();
21: mysql_close($mysql);
22: die("Failed Query");
23: }
24:
25: while ($line = mysql_fetch_assoc($result)) {
26; print_r($line);
27: }
28:
29: mysql_free_result($result);
30:
31: mysql_close($mysql);
32: ?>
33: </body>
34:</html>
35:
次は、リファクタリングだね
今のコードは、DBに強く依存していて、好ましくないの。
だから、少し変更するの。
これ以降のコードは、行番号は要らないので注意。
mysql.php
<?php
if(!defined("_MAIN_")) {
header("HTTP/1.1 404 Not Found");
die("Not Found");
}
/**
* MySQLデータベースを管理。
*/
class MySql {
var $config;
var $con;
/**
* 接続。
* @param <type> $config 設定情報。
*/
function Open($config) {
if ($config == NULL) {
throw new Exception("Required MySql Setting.");
}
$this->config = $config;
$this->con = mysql_connect($config->server, $config->user, $config->pwd);
if (!$this->con) {
throw new Exception("failed Connect MySql Server.");
}
}
/**
* 切断。
*/
function Close() {
mysql_close($this->con);
}
/**
* 内部用
*/
function _checkConnection() {
if (!$this->con) {
throw new Exception("Required Connect MySql Server.");
}
}
/**
* 内部用
* @param <type> $query
* @return <type>
*/
function _query($query) {
$resource = mysql_query($query, $this->con);
if (!$resource) {
throw new Exception(mysql_error());
}
return $resource;
}
/**
* 内部用
* @param <type> $resource
* @param <type> $func
* @return array
*/
function _fetch($query, $func) {
$this->_checkConnection();
$result = array();
$resource = $this->_query($query);
while ($line = $this->$func($resource)) {
array_push($result, $line);
}
mysql_free_result($resource);
return $result;
}
/**
* 内部用
* @param <type> $resource
* @return <type>
*/
function _array($resource) {
return mysql_fetch_row($resource);
}
function _single($resource) {
$result = mysql_fetch_row($resource);
return $result[0];
}
/**
* 内部用
* @param <type> $resource
* @return <type>
*/
function _hash($resource) {
return mysql_fetch_assoc($resource);
}
/**
* 結果に二重配列を用いる検索メソッド。
* @param <type> $query クエリ。
* @return <type> 結果。
*/
function queryArray($query) {
return $this->_fetch($query, '_array');
}
/**
* 結果にマップの配列を用いる検索メソッド。
* @param <type> $query クエリ。
* @return <type> 結果。
*/
function queryHash($query) {
return $this->_fetch($query, '_hash');
}
/**
* 結果のない操作用メソッド。
* @param <type> $query クエリ。
*/
function queryVoid($query) {
$this->_checkConnection();
$resource = $this->_query($query);
}
/**
*テーブル一覧を取得。
* @return <type> テーブル一覧。
*/
function getTables() {
return $this->_fetch("SHOW TABLES FROM " . $this->config->user, '_single');
}
}
?>
tables.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
define("_MAIN_", "1");
include './config.php';
include './mysql.php';
$config = new Config();
$mysql = new MySql();
$mysql->Open($config);
try {
print_r($mysql->getTables());
} catch(Exception $ex) {
print_r($ex);
}
$mysql->Close();
?>
</body>
</html>
index.phpも同じ感じで修正するの。
コードは省略。