Home  >  Article  >  Backend Development  >  How to connect to mysql database using perl through dbi_PHP tutorial

How to connect to mysql database using perl through dbi_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:32:401125browse

By using DBI, Perl can easily connect to the mysql database:

The code is as follows:

Copy the code The code is as follows:

#!/bin/perl

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:

Copy code The code is as follows:

[root@eygle ~]# perl test.cgi
eygle

In the future, many statistical data can be written directly into the Mysql database through Perl, making the operation much more convenient.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/755286.htmlTechArticleBy using DBI, Perl can be used to easily connect to the mysql database: The code is as follows: Copy the code The code is as follows: # !/bin/perl use DBI; # Connect to target DB my $dbh = DBI-connect("DB...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn