[PHP-users 8654] 列の型、表示AP(Postgres7.2)

小柳 俊一 php-users@php.gr.jp
Sun, 14 Jul 2002 23:17:11 +0900


PHPが好きな「orthtm@siren.ocn.ne.jp」です。

PHP4.2、PostgrsSQL7.2で実験したソースを公開します。
まだ、中途半端です。

PHP3で始めましたので、情報の引き継ぎはPost変数で引き継いでいます。

日付等、余分なコードもあります。(20点/100点です)

時間のある方、いじってみては!
================================================================================
AddApGn0.php
-----------------------------------------------------------------------------start
<html>
<head>
<title>PHP・AP生成・DB選択</title>
<meta http-equiv=Content-Type content="text/html; charset=euc-jp">
</head>
<SCRIPT language="JavaScript">
<!--
function InCheck() {
document.In0.submit();
}
//-->
</SCRIPT>
<body bgcolor="#EEE8AA">
<home>
<?php
//echo "a=[" . $a ."]";
$selection_date=date("Y/m/d"); //当日日付を取得
$now_year=substr($selection_date[0],0,4);
$now_month=substr($selection_date[0],5,2);
$now_day=substr($selection_date[0],8,2);
$selection_date=$now_year . "/" . $now_month ."/" . $now_day;
$conn = pg_connect("","","template1");
if ($conn == false) {
printf("データベース「$dbname」に接続出来ませんでした。\n");
exit;
}
$sql1 = "select datname from pg_database;";
$result1 = pg_exec($sql1);
if ($result1 == false) {
printf("レコードがありません!");
pg_close($conn);
exit;
}
$numrows1=pg_numrows($result1);
//echo "numrows:[" . $numrows1 . "]";
$maxcolum=pg_numfields($result1);
?>
<table border="1" align="center"><td BGCOLOR="#FFFF33">
◆◆追加処理PHP・DB選択◆◆</td></table>
<form ACTION="AddApGn1.php" METHOD=POST name="In0">
<p>DB名:
<select name="dbname1" size="1">
<option value="" selected>
<? for($k=0; $k<$numrows1; $k++) { ?>
<option value= "<? echo pg_result($result1,$k,'d
atname'); ?>" ><?php echo pg_result($result1,$k,'datname') . "\n"; ?>
<? } ?>
</select>
</p>
<!--<input type="button" name=ken value="実 行" onClick="InCheck()"> --
>
<input type="submit" name=ken value="実 行">

</form>
</home>
</body>
</html>
------------------------------------------------------------------------------end
AddApGn1.php
----------------------------------------------------------------------------start
<html>
<head>
<title>PHP・AP生成・TBL選択</title>
<meta http-equiv=Content-Type content="text/html; charset=euc-jp">
</head>
<SCRIPT language="JavaScript">
<!--
function InCheck() {
// dbname = document.In1.dbname.value;
// tblname = document.In1.tblname.value;
// if( dbname == "" || tblname == "") {
// alert("DB名、TBL名は省略出来ません。");
// } else {
document.In1.submit();
// }
}
//-->
</SCRIPT>
<body bgcolor="#EEE8AA">
<home>
<?
//echo "dbneme1[" . $_POST['dbname1'] . "]";
$dbname=$_POST['dbname1'];
$selection_date=date("Y/m/d"); //当日日付を取得
$now_year=substr($selection_date[0],0,4);
$now_month=substr($selection_date[0],5,2);
$now_day=substr($selection_date[0],8,2);
$selection_date=$now_year . "/" . $now_month ."/" . $now_day;
$ecode=i18n_http_input($dbname);
$dbname1=i18n_convert($dbname,$ecode);
//echo $ecode;
//echo "dbname[" . $dbname . "]";
//echo "<br>\n";
$conn = pg_connect("","","$dbname");
if ($conn == false) {
printf("データベース「$dbname」に接続出来ませんでした。\n");
exit;
}
//echo $dbname1;
//echo $tblname;
//$sql2 = "select relname,array_dims(relacl) from pg_class;";
//$sql2 = "select relname,array_dims(relacl) from pg_class where
relkind='r' and reltype =0 and array_dims(relacl)<>'';";
$sql2 = "select relname,array_dims(relacl) from pg_class where
array_dims(relacl)<>'';";
$result2 = pg_exec($sql2);
if ($result2 == false) {
printf("レコードがありません!");
pg_close($conn);
exit;
}
$numrows2=pg_numrows($result2);
if($numrows2==0) {
echo "データベース「" . $dbname . "」にはテーブルは有りませんでした。";
exit;
}
//echo "numrows:[" . $numrows2 . "]";
$maxcolum=pg_numfields($result2);
//echo "maxcolum:[" . $maxcolum . "]";
//echo pg_result($result2,0,'relname');
?>
<table border="1" align="center"><td BGCOLOR="#FFFF33">
◆◆追加処理PHP・AP生成◆◆</td></table>
<form ACTION="AddApGn2.php" METHOD=POST name="In1">
<p>DB名:<? echo $dbname ?>
</p>
<p>TBL名:
<select name="tblname" size="1">
<? for($k=0; $k<$numrows2; $k++) {
// if(pg_result($result2,$k,'array_dims')<>"") { ?>
<option value= <? echo pg_result($result2,$k,'relname'); ?> ><? echo
pg_result($result2,$k,'relname'); ?>
<? //} ?>
<? } ?>
</select>
</p>
<br>
<input type="hidden" name="dbname" value=<? echo $dbname ?>>
</p>
<input type="button" name=ken value="実 行" onClick="InCheck()">
<br> 
<br>上記のデータベース名、テーブル名で実行可能です。

</form>
</body>
</home>
----------------------------------------------------------------------------------------------------end
AddApGn2.php
----------------------------------------------------------start
<html>
<head>
<title>追加処理PHP・AP生成・列表示</title>
<meta http-equiv=Content-Type content="text/html; charset=euc-jp">
</head>
<SCRIPT language="JavaScript">
<!--
function InCheck() {
var chkcd = 0;
if(chkcd==0){
document.In2.submit();
}
}
//-->
</SCRIPT>
<body bgcolor="#EEE8AA">
<home>
<?
$selection_date=date("Y/m/d"); //当日日付を取得
$now_year=substr($selection_date[0],0,4);
$now_month=substr($selection_date[0],5,2);
$now_day=substr($selection_date[0],8,2);
$selection_date=$now_year . "/" . $now_month ."/" . $now_day;
$dbname=$_POST['dbname'];
//echo "dbname[" . $dbname . "]";
$conn = pg_connect("","","$dbname");
if ($conn == false) {
printf("データベース「$dbname」に接続出来ませんでした。\n");
exit;
}
$tblname=$_POST['tblname'];
//echo "tblname[" . $tblname . "]";
//$ecode=i18n_http_input($tblname);
//echo $ecode;
//$tblname=i18n_convert($tblname,$ecode);
//echo $dbname;
//echo $tblname;
$sql = "SELECT pg_class.relname,pg_attribute.attname,pg_attribute.attlen," .
"pg_attribute.atttypmod,pg_attribute.attnum," .
"pg_attribute.attalign,pg_attribute.attnotnull,pg_type.typname,pg_type.typlen
" .
"FROM pg_class,pg_attribute,pg_type " .
"WHERE pg_class.oid = pg_attribute.attrelid and " .
"pg_attribute.atttypid = pg_type.oid and " .
"pg_class.relname='" . $tblname . "' and pg_attribute.attnum > 0 " .
"ORDER BY pg_attribute.attnum;";
//$sql = "select * from "
// . $tblname . ";";
//echo $sql;
$result = pg_exec($sql);
if ($result == false) {
printf("レコードがありません!");
pg_close($conn);
exit;
}
$numrows=pg_numrows($result);
$maxcolum=$numrows;
//echo "numrows:[" . $numrows . "]<br>";
$maxcolum=pg_numfields($result);
//echo "maxcolum[" . $maxcolum . "]<br>";
//echo pg_result($result,0,'attname');
//echo pg_result($result,0,'attnotnull');
?>
<table border="1" align="left" width="100%"><td colspan=4
BGCOLOR="#FFFF33" align="center">
◆◆ 追加処理PHP・列名表示 DB名:<? echo $dbname; ?> TBL名:<?
echo $tblname ?> ◆◆</td></tr>
<form ACTION="AddApGn3.php" METHOD=POST name="In2">
<td>列名</td><td>型</td><td>byte数</td><td>入力項目</td></tr>
<?
for($l=0;$l<=$numrows-1;$l++) {
echo "<td>"; echo pg_result($result,$l,'attname');
if(pg_result($result,$l,'attnotnull')=="t") { echo "(必須)"; } echo
"</td>\n";
echo "<td>"; echo pg_result($result,$l,'typname'); echo "</td>\n";
if(pg_result($result,$l,'typlen')== -1) {
if(pg_result($result,$l,'atttypmod')== -1) {
echo "<td>可変長</td>\n";
$len=80;
$maxlen=256;
$maxlen2=1024;
} else {
$len = pg_result($result,$l,'atttypmod')-4;
$maxlen=$len;
?><td><? echo $len; ?></td><?
}
if($maxlen>79) {
$row = round($maxlen/54);
?><td><textarea rows="<? echo $row ?>" cols="80" maxlength="<? echo
$maxlen2; ?>" name="<? echo pg_result($result,$l,'attname');
?>"></textarea></td><? echo "\n";
} else {
?><td><input type="text" name="<? echo pg_result($result,$l,'attname');
?>" size="<? echo $len; ?>" maxlength="<? echo $maxlen; ?>"></td><? echo
"\n";
}
} else {
echo "<td>"; echo pg_result($result,$l,'typlen'); echo "</td>\n";
?><td><input type="text" name="<? echo pg_result($result,$l,'attname');
?>" size="<? echo $len; ?>" maxlength="<? echo $maxlen; ?>"></td><? echo
"\n";
}
echo "</tr>\n";
}
for($l=0;$l<=$numrows-1;$l++) {
?><input type="hidden" name="fname<? echo $l; ?>" value="<? echo
pg_result($result,$l,'attname'); ?>"><? echo "\n";
?><input type="hidden" name="ftype<? echo $l; ?>" value="<? echo
pg_result($result,$l,'typname'); ?>"><? echo "\n";
if(pg_result($result,$l,'typlen')== -1) {
if(pg_result($result,$l,'atttypmod')== -1) {
?><input type="hidden" name="fsize<? echo $l; ?>" value="可変長"> <?
echo "\n";
} else {
$len = pg_result($result,$l,'atttypmod')-4;
$maxlen=$len;
?><input type="hidden" name="fsize<? echo $l; ?>" value="<? echo $len;
?>"> <? echo "\n";
}
} else {
?><input type="hidden" name="fsize<? echo $l; ?>" value="<? echo
pg_result($result,$l,'typlen'); ?>"> <? echo "\n";
}
}
?><input type="hidden" name="maxclm" value="<? echo $numrows; ?>"><?
echo "\n";
?><input type="hidden" name="dbname" value="<? echo $dbname; ?>"><? echo
"\n";
?><input type="hidden" name="tblname" value="<? echo $tblname; ?>"><?
echo "\n";
?>
<td colspan=4 align="center"><input type="button" name=ken2 value="追加
更新" onClick="InCheck()"></td></tr>
<td colspan=4 align="center">
<? echo "「追加更新」ボタンを押してもDBは更新しません!" ;?>
</td></tr>
</table>
</form>
<? pg_close($conn); ?>
</home>
</bo<html>
<head>
<title>追加処理PHP・AP生成・列表示(AddApGn2.php3)</title>
<meta http-equiv=Content-Type content="text/html; charset=euc-jp">
</head>
<SCRIPT language="JavaScript">
<!--
function InCheck() {
var chkcd = 0;
if(chkcd==0){
document.In2.submit();
}
}
//-->
</SCRIPT>
<body bgcolor="#EEE8AA">
<home>
<?
$selection_date=date("Y/m/d"); //当日日付を取得
$now_year=substr($selection_date[0],0,4);
$now_month=substr($selection_date[0],5,2);
$now_day=substr($selection_date[0],8,2);
$selection_date=$now_year . "/" . $now_month ."/" . $now_day;
$dbname=$_POST['dbname'];
//echo "dbname[" . $dbname . "]";
$conn = pg_connect("","","$dbname");
if ($conn == false) {
printf("データベース「$dbname」に接続出来ませんでした。\n");
exit;
}
$tblname=$_POST['tblname'];
//echo "tblname[" . $tblname . "]";
//$ecode=i18n_http_input($tblname);
//echo $ecode;
//$tblname=i18n_convert($tblname,$ecode);
//echo $dbname;
//echo $tblname;
$sql = "SELECT pg_class.relname,pg_attribute.attname,pg_attribute.attlen," .
"pg_attribute.atttypmod,pg_attribute.attnum," .
"pg_attribute.attalign,pg_attribute.attnotnull,pg_type.typname,pg_type.typlen
" .
"FROM pg_class,pg_attribute,pg_type " .
"WHERE pg_class.oid = pg_attribute.attrelid and " .
"pg_attribute.atttypid = pg_type.oid and " .
"pg_class.relname='" . $tblname . "' and pg_attribute.attnum > 0 " .
"ORDER BY pg_attribute.attnum;";
//$sql = "select * from "
// . $tblname . ";";
//echo $sql;
$result = pg_exec($sql);
if ($result == false) {
printf("レコードがありません!");
pg_close($conn);
exit;
}
$numrows=pg_numrows($result);
$maxcolum=$numrows;
//echo "numrows:[" . $numrows . "]<br>";
$maxcolum=pg_numfields($result);
//echo "maxcolum[" . $maxcolum . "]<br>";
//echo pg_result($result,0,'attname');
//echo pg_result($result,0,'attnotnull');
?>
<table border="1" align="left" width="100%"><td colspan=4
BGCOLOR="#FFFF33" align="center">
◆◆ 追加処理PHP・列名表示 DB名:<? echo $dbname; ?> TBL名:<?
echo $tblname ?> ◆◆</td></tr>
<form ACTION="AddApGn3.php" METHOD=POST name="In2">
<td>列名</td><td>型</td><td>byte数</td><td>入力項目</td></tr>
<?
for($l=0;$l<=$numrows-1;$l++) {
echo "<td>"; echo pg_result($result,$l,'attname');
if(pg_result($result,$l,'attnotnull')=="t") { echo "(必須)"; } echo
"</td>\n";
echo "<td>"; echo pg_result($result,$l,'typname'); echo "</td>\n";
if(pg_result($result,$l,'typlen')== -1) {
if(pg_result($result,$l,'atttypmod')== -1) {
echo "<td>可変長</td>\n";
$len=80;
$maxlen=256;
$maxlen2=1024;
} else {
$len = pg_result($result,$l,'atttypmod')-4;
$maxlen=$len;
?><td><? echo $len; ?></td><?
}
if($maxlen>79) {
$row = round($maxlen/54);
?><td><textarea rows="<? echo $row ?>" cols="80" maxlength="<? echo
$maxlen2; ?>" name="<? echo pg_result($result,$l,'attname');
?>"></textarea></td><? echo "\n";
} else {
?><td><input type="text" name="<? echo pg_result($result,$l,'attname');
?>" size="<? echo $len; ?>" maxlength="<? echo $maxlen; ?>"></td><? echo
"\n";
}
} else {
echo "<td>"; echo pg_result($result,$l,'typlen'); echo "</td>\n";
?><td><input type="text" name="<? echo pg_result($result,$l,'attname');
?>" size="<? echo $len; ?>" maxlength="<? echo $maxlen; ?>"></td><? echo
"\n";
}
echo "</tr>\n";
}
for($l=0;$l<=$numrows-1;$l++) {
?><input type="hidden" name="fname<? echo $l; ?>" value="<? echo
pg_result($result,$l,'attname'); ?>"><? echo "\n";
?><input type="hidden" name="ftype<? echo $l; ?>" value="<? echo
pg_result($result,$l,'typname'); ?>"><? echo "\n";
if(pg_result($result,$l,'typlen')== -1) {
if(pg_result($result,$l,'atttypmod')== -1) {
?><input type="hidden" name="fsize<? echo $l; ?>" value="可変長"> <?
echo "\n";
} else {
$len = pg_result($result,$l,'atttypmod')-4;
$maxlen=$len;
?><input type="hidden" name="fsize<? echo $l; ?>" value="<? echo $len;
?>"> <? echo "\n";
}
} else {
?><input type="hidden" name="fsize<? echo $l; ?>" value="<? echo
pg_result($result,$l,'typlen'); ?>"> <? echo "\n";
}
}
?><input type="hidden" name="maxclm" value="<? echo $numrows; ?>"><?
echo "\n";
?><input type="hidden" name="dbname" value="<? echo $dbname; ?>"><? echo
"\n";
?><input type="hidden" name="tblname" value="<? echo $tblname; ?>"><?
echo "\n";
?>
<td colspan=4 align="center"><input type="button" name=ken2 value="追加
更新" onClick="InCheck()"></td></tr>
<td colspan=4 align="center">
<? echo "「追加更新」ボタンを押してもDBは更新しません!" ;?>
</td></tr>
</table>
</form>
<? pg_close($conn); ?>
</home>
</body>
</html>
--------------------------------------------------------------------------end
AddApGn3.php
----------------------------------------------------------start
<html>
<head>
<title>追加処理PHP・AP生成・更新したいな!</title>
<meta http-equiv=Content-Type content="text/html; charset=euc-jp">
</head>
<SCRIPT language="JavaScript">
<!--
function InCheck() {
var chkcd = 0;
if(chkcd==0){
document.In3.submit();
}
}
//-->
</SCRIPT>
<body bgcolor="#EEE8AA">
<home>
<?
$dbname=$_POST['dbname'];
$selection_date=date("Y/m/d"); //当日日付を取得
$now_year=substr($selection_date[0],0,4);
$now_month=substr($selection_date[0],5,2);
$now_day=substr($selection_date[0],8,2);
$selection_date=$now_year . "/" . $now_month ."/" . $now_day;
$conn = pg_connect("","","$dbname");
if ($conn == false) {
printf("データベース「$dbname」に接続出来ませんでした。\n");
exit;
}
$tblname=$_POST['tblname'];
//$ecode=i18n_http_input($tblname);
//echo $ecode;
//$tblname=i18n_convert($tblname,$ecode);
//echo $tblname;
$sql = "select * from " . $tblname . ";";
//$result = pg_exec($sql);
//if ($result == false) {
// printf("レコードがありません!");
// pg_close($conn);
// exit;
//}
//$maxcolum=pg_numfields($result);
?>
<table border="1" align="left" width="100%"><td colspan=4
BGCOLOR="#FFFF33" align="center">
◆◆ テーブル追加内容表示 DB名:<? echo $dbname; ?> TBL名:<? echo
$tblname ?> ◆◆</td></tr>
<form ACTION="AddApGn4.php3" METHOD=POST name="In3">
<td>列名</td><td>型</td><td>byte数</td><td>追加内容</td></tr>
<?
$maxclm=$_POST['maxclm'];
//echo $_POST['fname1'] . "<br>";
for($columno=0;$columno<=$maxclm-1;$columno++) {
$fn="fname" . $columno;
$ft="ftype" . $columno;
$fs="fsize" . $columno;
$fc=${$_POST[$fn]};
echo "<td>"; echo $_POST[$fn]; echo "</td>\n";
echo "<td>"; echo $_POST[$ft]; echo "</td>\n";
echo "<td>"; echo $_POST[$fs]; echo "</td>\n";
if($_POST[$_POST[$fn]]=="") {
echo "<td> </td>\n";
} else {
echo "<td>"; echo $_POST[$_POST[$fn]]; echo "</td>\n";
}
echo "</tr>\n";
}
?><input type="hidden" name="maxclm" value="<? echo $maxcolum; ?>"> <?
echo "\n";
?><input type="hidden" name="dbname" value="<? echo $dbname; ?>"> <?
echo "\n";
?><input type="hidden" name="tblname" value="<? echo $tblname; ?>"> <?
echo "\n";
?>
</table>
</form>
<? pg_close($conn); ?>
</home>
------------------------------------------------------------------------------end
以上です