Perl
  Home arrow Perl arrow Page 4 - SQL and CGI with Perl and DBI
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

SQL and CGI with Perl and DBI
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-04-03


    Table of Contents:
  • SQL and CGI with Perl and DBI
  • A More Complex Example
  • Use Placeholders
  • DBI and Table Joins
  • Perl DBI CGI = Fun!
  • What We Didn’t Talk About

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    SQL and CGI with Perl and DBI - DBI and Table Joins
    ( Page 4 of 6 )

    Any SQL query is possible using DBI. This includes table joins. Let’s modify the previous example showinstruments2.pl and perform a table join as shown here in showinstrument3.pl:

    #!/usr/bin/perl - w
    # showinstruments3.pl

    use strict;
    use DBI;

    my($who, $instrument);

    print "Enter name of musician and I will show you his/her instruments: ";
    chomp($who = <STDIN>);

    my $dbh = DBI->connect("DBI:mysql:musicians_db", "musicfan", "CrimsonKing");

    die "connect failed: " . DBI->errstr() unless $dbh;

    # use a table join to query the instrument names
    my $sth = $dbh->prepare("SELECT instruments.instrument
        FROM musicians,what_they_play,instruments
        WHERE musicians.name = ? AND

            musicians.player_id = what_they_play.player_id AND  
            what_they_play.inst_id = instruments.inst_id")
                 or die "prepare failed: " . $dbh->errstr();

    $sth->execute($who) or die "execute failed: " . $sth->errstr();

    # loop through them, printing them
    while (($instrument) = $sth->fetchrow()) {
        print "    $instrument\n";
    }

    $sth->finish();

    $dbh->disconnect();

    The big change is the preparation and execution of the query:

    # use a table join to query the instrument names
    my $sth = $dbh->prepare("SELECT instruments.instrument
        FROM musicians,what_they_play,instruments
        WHERE musicians.name = ? AND
            musicians.player_id = what_they_play.player_id AND
            what_they_play.inst_id = instruments.inst_id")
                 or die "prepare failed: " . $dbh->errstr();

    $sth->execute($who) or die "execute failed: " . $sth->errstr();

    Here we construct one large query as we did previously in this chapter. It joins the musicians , what_they_play , and instruments tables. Notice how we are using a placeholder when we compare musicians.name and how the variable $who is provided within the execute() method.

    Does this table join work? Yep.

    $ perl showinstruments3.pl
    Enter name of musician and I will show you his/her instruments: Thom Yorke
        keyboards 
        guitar
        vocals
    $

    Perl and DBI allow us to easily create programs that query our database. This means we can do anything with Perl that we can do with the SQL database including many SQL commands that we have not talked about in this chapter.



     
     
    >>> More Perl Articles          >>> More By Apress Publishing
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT