host=news password=foobar
学内からしか使えません
<form action="psql.php" method="post">
SQL:<br>
<textarea name="sql" rows="5" cols="60"></textarea><br>
<input type="submit" value="Query">
<input type="reset" value="Clear">
</form>
<?php
if (!preg_match("/^(192\.244\.75\.|10\.|127\.)/", $REMOTE_ADDR)) {
echo "<p>学内からしか使えません\n";
} elseif ($_POST['sql']) {
$s = htmlspecialchars($_POST['sql'], ENT_QUOTES);
echo "<p>SQL: $s\n";
$conn = pg_connect("host=news password=foobar");
if ($conn == FALSE) {
echo "<p>データベースに接続できませんでした";
} else {
$result = pg_query($conn, $s);
if ($result == FALSE) {
echo "<p>SQL エラー";
} else {
$rows = pg_num_rows($result);
$cols = pg_num_fields($result);
if ($cols > 0) {
echo "<p><table border=\"1\">\n";
echo "<tr>";
for ($j = 0; $j < $cols; $j++) {
$s = htmlspecialchars(pg_field_name($result, $j));
echo "<th>$s";
}
echo "</tr>\n";
for ($i = 0; $i < $rows; $i++) {
echo "<tr>";
for ($j = 0; $j < $cols; $j++) {
$s = htmlspecialchars(pg_fetch_result($result, $i, $j));
echo "<td>$s";
}
echo "</tr>\n";
}
echo "</table>\n";
}
echo "<p>Ok.\n";
pg_freeresult($result);
}
pg_close($conn);
}
}
?>
松阪大学 奥村晴彦 okumura@matsusaka-u.ac.jp
Last modified: 2004-11-08 08:56:21