|
楼主 |
发表于 2005-8-1 13:56:24
|
显示全部楼层
通过DBI模块访问mysql
[PHP]
#!/usr/bin/perl -w
#use DBI module
use DBI;
#define $dsn="DBI:database-type(cann't set with mssql):hostname";
my $dsn="DBI:mysql:mydata:localhost";
my $db_user="root";
my $db_pass="";
#set database handle $dbh
$dbh=DBI->connect($dsn,$db_user,$db_pass,{RaiseError=>1});
#set string handle $sth
######select from table and print(have data back);
$sth=$dbh->prepare("select * from address");
$sth->execute();
while ( @row = $sth->fetchrow_array ) {
print "@row\n";
}
$sth->finish();
######insert into table and print(no data back doesn't need $str->finish();
$sth=$dbh->prepare("insert into address(id,name,email,telephone) values(2,'test2','test2@test2.com',12345678)");
$sth->execute();
$dbh->disconnect();
[/PHP] |
|