Disclaimer: Its a collection from lots of other site(s) and few of my notes. I would also like to declare that I am not owning lots of its content. Please feel free to contact me directly if you want me to remove any of your content, that you don't want to share to other through this blog.
Sunday, 13 July 2014
perl_mysql_connect_example
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
#Setp1 - creating connection object
my $dsn = 'DBI:mysql:thenewboston'; # module:db_engine:db_name
my $user = 'Username';
my $pass = 'Password';
my $host = 'localhost';
my $conn = DBI->connect($dsn,$user,$pass) || die "Error connecting" . DBI->errstr;
# step 2 - define query
my $query1 = $conn->prepare('SELECT * FROM user') || die "Error preparing query" . $conn->errstr;
# setp 3 - execute query
$query1->execute || die "Error executing query" . $conn->errstr;
# step 4 - return results
my @results;
while (@results = $query1->fetchrow_array()) {
my $id = $results[0];
my $name = $results[1];
print "$id,$name\n"
}
Labels:
perl
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment