![]() | PHP & MySQL |
|
|
|
|||
「ゼロから学ぶWebプログラミング」 日経ソフトウェア編 日経BP社
「PHPポケットリファレンス」 大垣靖男著 技術評論社
「SQLポケットリファレンス」 朝井淳著 技術評論社| 例: | 会員制サイト |
| ショッピングカート |
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=Shift_JIS">
<title>訪問者の表示</title>
</head>
<body>
<div align="center"
<p align="center">ユーザー名を入力してください</p>
<form method="POST" name="form" action="index.php" enctype="multipart/form-data">
ユーザー名:<input type="text" name="id">
<input type="submit" value="OK">
</form>
</div>
</body>
</html>
<?php
if(!$_POST['id']){
exit;
}
session_name('VISITOR');
session_cache_limiter('nocache');
session_start();
$_SESSION['id'] = addslashes($_POST['id']);
header('Location: page1.php');
?>
<?php
session_name('VISITOR');
session_cache_limiter('nocache');
//セッション開始
session_start();
if(!$_SESSION['id']){
session_destroy();
setcookie('VISITOR','');
header('Location: index.php');
exit;
}
//訪問者の表示
$_SESSION['url'] = $_SERVER['PHP_SELF'];
print('<h2 style="color:blue">ページ1</h2><br>');
print('このページの訪問者<br><br>');
$dir = dir(session_save_path());
while(($ent = $dir->read()) !== false){
if(!ereg('\.',$ent)){
$sess_file = session_save_path()."/".$ent;
if($fp = fopen($sess_file, "r")) {
$sess_data = fread($fp, filesize($sess_file));
$s = explode('"',$sess_data);
if($s[3] == $_SERVER['PHP_SELF']){
print($s[1]." さん<br>");
}
fclose($fp);
}
}
}
print($_SESSION['id']." さん<br>");
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=Shift_JIS">
<title>訪問者の表示</title>
</head>
<body>
<br>
<a href="page1.php">ページ1</a> <a href="logout.php">ログアウト</a>
</body>
</html>
<?php
session_name('VISITOR');
session_cache_limiter('nocache');
session_start();
session_destroy();
setcookie('VISITOR','');
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=Shift_JIS">
<title>訪問者の表示</title>
</head>
<body>
<div align="center" style="margin-top:50px">ログアウトしました<br><br>
<b><span style="color:blue">
<u onClick="window.history.back()"
onMouseover="this.style.cursor='hand'">戻る</u>
<u onClick="window.close()"
onMouseover="this.style.cursor='hand'">閉じる</u>
</span></b>
</div>
</body>
</html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=Shift_JIS">
<title>MYSQLのデータ表示</title>
<style type="text/css"><!--
body {font-family:'MS ゴシック'}
--></style>
<script language="JavaScript"><!--
db = new Array();
tbl = new Array();
for(i=0;i<100;i++){
tbl[i] = new Array();
}
<?php
//*******************************************************************************
$server = 'localhost';
$user = 'root';// MySQLのユーザー名を入れます
$pass = ''; // MySQLのパスワードを入れます
//*******************************************************************************
$con = mysql_connect($server, $user, $pass);
if(!$con){
exit("$serverに接続できません。<br>");
}
$dbs = mysql_List_dbs($con);
$i = 0;
while($row = mysql_fetch_row($dbs)){
print'db['.$i.'] = "'.$row[0].'";';
mysql_select_db($row[0],$con);
$tbl = mysql_List_tables($row[0],$con);
$j = 0;
while($row = mysql_fetch_row($tbl)){
print'tbl['.$i.']['.$j.'] = "'.$row[0].'";';
$j++;
}
$i++;
}
mysql_free_result($dbs);
mysql_free_result($tbl);
mysql_close($con);
?>
function db_show()
{
var html = '<select name="n_db" onChange="tbl_show(this.selectedIndex)">';
for(i=0;i<db.length;i++){
html += '<option value='+db[i]+'>'+db[i];
}
html += '</select>';
document.getElementById('t_db').innerHTML = html;
}
function tbl_show(n)
{
var html = '<select id="n_tbl" name="n_tbl">';
for(i=0;i<tbl[n].length;i++){
html += '<option value='+tbl[n][i]+'>'+tbl[n][i];
}
html += '</select>';
document.getElementById('t_tbl').innerHTML = html;
}
--></script>
</head>
<body onLoad="db_show();tbl_show(0);">
<div align="center">
<form action="show_mysql.php" method="POST" enctype="multipart/form-data">
<table border="0">
<tr>
<td align="right">データベース名:</td>
<td><div id="t_db"></div></td>
</tr>
<tr>
<td align="right">テーブル名:</td>
<td><div id="t_tbl"></div></td>
</tr>
<tr>
<td align="right"><input type="submit" value="データ表示"></td>
<td></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
// データの表示
if(!$_POST['n_db'] || !$_POST['n_tbl']){
exit;
}
$_POST['n_db'] = mysql_escape_string($_POST['n_db']);
$_POST['n_tbl'] = mysql_escape_string($_POST['n_tbl']);
$db = $_POST['n_db'];
$tbl = $_POST['n_tbl'];
print"データベース名:";
print_r($_POST['n_db']);
print"<br>テーブル名:";
print_r($_POST['n_tbl']);
print"<br><br>";
$con = mysql_connect($server, $user, $pass);
if(!$con){
exit("$serverに接続できません。<br>\n");
}
mysql_select_db($db,$con);
$sql = "select * from ".$tbl;
$res = mysql_query($sql);
if(!$res){
exit("データを取り出せません。<br>\n");
}
while($row = mysql_fetch_assoc($res)){
print_r($row);
print"<br><br>";
}
mysql_free_result($res);
mysql_close($con);
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=EUC-JP">
<title>郵便番号</title>
</head>
<body>
<?php
$server = 'localhost'; // サーバー名
$user = 'root'; // ユーザー名
$pass = ''; // パスワード
$db = 'zipcode'; // データベース名
$tn = 'tbl_1'; // テーブル名
$con = mysql_connect($server, $user, $pass);
if(!$con){
exit("".$server." に接続できません。<br>\n");
}
$r = mysql_query("CREATE DATABASE ".$db);
mysql_select_db($db);
$r = mysql_query("CREATE TABLE ".$tn." (
xzip varchar(255) not null,
xaddr1 varchar(255) not null
)");
function addCsv($fn,$tn){
$fp = fopen($fn,'r');
if(!$fp){
mysql_close($db);
exit("ファイルの読込失敗 !<br>\n");
}
while($res = fgetcsv($fp,1024)){
for($i=2;$i<9;$i++){
$res[$i] = mysql_escape_string($res[$i]);
}
$s1 = '"'.$res[2].'","'.$res[6].$res[7].$res[8].'"';
$r = mysql_query("INSERT INTO ".$tn." VALUES (".$s1.")");
if($r){
print(strval($res[2]).'<br>');
}
}
}
mysql_query("TRUNCATE TABLE ".$tn);
addCsv('C:\\KEN_ALL.CSV',$tn); // データファイルは、サーバのCドライブに置きます。
mysql_close($con);
?>
</body>
</html>
![]() | ![]() |