Home > Article > Backend Development > How to connect to mysql database using perl through dbi_PHP tutorial
By using DBI, Perl can easily connect to the mysql database:
The code is as follows:
use DBI;
# Connect to target DB
my $dbh = DBI->connect("DBI:mysql:database=eygle;host=localhost","username","password", {'RaiseError' => 1});
# Insert one row
my $rows = $dbh->do("INSERT INTO test (id, name) VALUES (1, 'eygle')");
# query
my $sqr = $dbh->prepare("SELECT name FROM test");
$sqr->execute();
while(my $ref = $sqr->fetchrow_hashref()) {
print "$ref->{'name'}n";
}
$dbh->disconnect();
Execution result:
In the future, many statistical data can be written directly into the Mysql database through Perl, making the operation much more convenient.