Welcome to Programmer's Room   

Perl-データベース(DBI)
Pg.pmを使用したもうひとつのPostgresqlに接続する方法です。


#!/usr/local/bin/perl

use Pg;
use CGI qw/:standard/;

print header(-charset=>'shift_JIS'),
    start_html(-title=>param('prog'), -lang=>'ja_JP', -encoding => 'Shift_JIS',
               -style=>{-code=> "body {margin-left:90px}"}),
    h3('Program Test'), p({-style=>'margin-left:90px;'});
# データベースへの接続
$conn = Pg::connectdb("user=prettycat password=a8653749 dbname=prettycat");

$sql = "SELECT * FROM accesslog order by num";

# SQL の発行
$result = $conn->exec($sql);

# 行数、フィールド数の取得
$numOfRows = $result->ntuples;
$numOfFields = $result->nfields;

# 内容をすべて表示。
while( @a = $result->fetchrow) {
    for($i = 0; $i < $numOfFields; $i++){
       print $a[$i] . "\t";
    }
    print "
"; } print end_html;

【 戻 る 】