#!/usr/bin/perl use DBI; require 'jcode.pl'; $user = 'test'; $passwd = 'test2001'; $db = DBI->connect('DBI:mysql:ATMARKIT:localhost', $user, $passwd); # FORMからデータを読み込む $len = $ENV{'CONTENT_LENGTH'}; read(STDIN, $std, $len); @args = split(/&/, $std); foreach $arg ( @args ){ ($name, $val ) = split(/=/, $arg); $val =~ s/\+/ /g; $val =~ s/%([0-9a-f][0-9a-f])/pack('C', hex($1))/egi; &jcode'convert(*val, 'euc'); $form{$name} = $val; } print < Sample MySQL+Perl HEADER if($form{command} eq 'select' || $form{command} eq 'delete'){ if($form{name} ne '' ){ $where .= "name like '\%$form{name}\%'"; } if($form{name} ne '' && $form{memo} ne ''){ $where .= " and " } if($form{memo} ne '' ){ $where .= "memo like '\%$form{memo}\%'"; } if($where ne ''){ $where = "WHERE " . $where; } if($form{command} eq 'select'){ $sth = $db->prepare("SELECT name, memo FROM list $where"); $sth->execute; $num_rows = $sth->rows; print "

該当 $num_rows 件

"; for ($i=0; $i<$num_rows; $i++) { @a = $sth->fetchrow_array; print "

name=$a[0] memo=$a[1]

"; } $sth->finish; } if($form{command} eq 'delete'){ $sth = $db->prepare("delete FROM list $where"); $sth->execute; print "

削除完了

"; $sth->finish; } }elsif($form{command} eq 'insert'){ if($form{name} eq '' ){ print "

nameが空白では登録できません

"; }else{ $sth = $db->prepare("insert into list (name,memo) values ('$form{name}','$form{memo}')"); $sth->execute; print "

登録完了

"; $sth->finish; } } $db->disconnect; print < FOOTER