MySQL Basics In Pictures
  1. Within your home directory, navigate to the programs directory.
  2. Save the blank document as presidents.pl in the programs directory.

  3. Highlight the code below, copy it, then paste it into presidents.pl.

    
    #!/usr/bin/perl
    
    use DBI;
    use strict;
    
    # database information
    my $db="us_presidents";
    my $host="localhost";
    my $port="3306";
    my $userid="marty";
    my $passwd="watch4keys";
    my $connectionInfo="DBI:mysql:database=$db;$host:$port";
    
    # make connection to database
    my $dbh = 
    DBI->connect($connectionInfo,$userid,$passwd);
    
    # prepare and execute query
    my $query = "SELECT id,first,middle,last FROM name ORDER BY id";
    my $sth = $dbh->prepare($query);
    $sth->execute(); 
    
    # assign fields to variables
    my ($id,$first,$middle,$last);
    $sth->bind_columns(undef, \$id, \$first, \$middle, \$last); 
    
    # output president's names listing
    print "The presidents in order:\n";
    while($sth->fetch()) {
       print "$first ";
       print "$middle " if ($middle);
       print "$last\n";
    } 
    
    # clean up
    $sth->finish();
    
    # disconnect from database
    $dbh->disconnect;