連携動作確認
1.テーブルを作成するSQL文を記述したファイルを作成します。
#su - postgres
$vi ~/customer.tab
CREATE TABLE customer (
no integer PRIMARY KEY,
name text NOT NULL,
mail text
);
2.データベースを作成します。(ここではデータベース名をwwwdbとします。)
$createdb wwwdb
3.データベースwwwdbに先ほど作成したファイルを使ってテーブルを作成します。
$psql wwwdb < ~/customer.tab
4.データベースに接続します。(psqlはPostgreSQLに付属されているツールで、データベースを操作するための対話的ターミナルです。)
$psql wwwdb
5.テーブルが作成されているか確認します。
wwwdb=#\d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | customer | table | postgres
(1 row)
wwwdb=#\d customer
Table "public.customer"
Column | Type | Modifiers
--------+---------+-----------
no | integer | not null
name | text | not null
mail | text |
Indexes:
"customer_pkey" primary key, btree ("no")
6.customerテーブルにデータを挿入します。
wwwdb=# insert into customer values(1,'田中太郎','taro@a.com');
wwwdb=# insert into customer values(2,'林健二','kenji@b.com');
wwwdb=# insert into customer values(3,'中島智之','tomoyuki@a.com');
7.customerテーブルにデータが挿入されているか確認します。
wwwdb=#select * from customer;
no | name | mail
----+----------+----------------
1 | 田中太郎 | taro@a.com
2 | 林健二 | kenji@b.com
3 | 中島智之 | tomoyuki@a.com
(3 rows)
8.インターネットからcustomerテーブルにアクセスするユーザ(createuserコマンドで作成したユーザ)にselect権限を与えます。
wwwdb=#grant select on customer to netuser;
9.psqlを終了します。
wwwdb=#\q
10.動作確認用の簡単なPHPスクリプトファイルを作成します。(ここでは/usr/local/apache/htdocs/をApacheのルートディレクトリとします。)
$vi /usr/local/apache/htdocs/customer.php
-----------------------------------------------------------------
<html>
<head>
<title>テスト</title>
</head>
<body>
<?php
$dbconn = pg_connect ("dbname=wwwdb user=netuser");
$result = pg_query ($dbconn, "SELECT * FROM customer ORDER BY no");
print "
<table border=\"1\">";
for ($i = 0; $i < pg_numrows($result); $i++) {
$arr = pg_fetch_assoc ($result, $i);
print "
<tr>
<td>" . $arr["no"] . "</td><td>" . $arr["name"] . "</td><td>" . $arr["mail"] . "</td>
</tr>";
}
pg_close($dbconn);
?>
</table>
</body>
</html>
-----------------------------------------------------------------
11.ブラウザからURLを指定してアクセスします。http://サーバのIPアドレス/customer.php
-----------------------------------------------------------------
最終更新:2007年12月25日 16:32