Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.00

HelloMySQL.pl - My First Perl Program with MySQL

This section describes a tutorial example on how use the DBI->connect() function to connect Perl scripts to MySQL database server.

Now we are ready to write a Perl program using the DBI module to access MySQL server. Here my first example, HelloMySQL.pl:

# HelloMySQL.pl
#- Copyright (c) 1999 by Dr. Herong Yang, http://www.herongyang.com/
#
   use DBI;
   $dbh = DBI->connect("DBI:mysql:test");
   $dbh->do("create table hello (message varchar(80))");
   $dbh->do("insert into hello (message) values ('Hello world!')");
   $sth = $dbh->prepare("select * from hello");
   $sth->execute();
   while ((@row) = $sth->fetchrow_array()) {
      print "$row[0]\n";
   }
   $sth->finish();
   $dbh->do("drop table hello");
   $dbh->disconnect();
   exit;

Let's start MySQL server, and run HelloMySQL.pl:

\perl\bin\perl HelloMySQL.pl
Hello world!

Congratulations, all required software and additional modules are working.

Sections in This Chapter

Installing Database Module for MySQL

HelloMySQL.pl - My First Perl Program with MySQL

Dr. Herong Yang, updated in 2008
HelloMySQL.pl - My First Perl Program with MySQL