Perl
  Home arrow Perl arrow Page 4 - SQL and CGI with Perl and DBI
FaxWave - Free Trial.
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 2
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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 themusicians,what_they_play, andinstrumentstables. Notice how we are using a placeholder when we comparemusicians.nameand how the variable$whois provided within theexecute()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 andDBIallow 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


       · This article is an excerpt from the book "Beginning Perl," published by Apress. We...
     

    Buy this book now. This article is excerpted from chapter 15 of the book Beginning Perl (Apress; ISBN: 159059391X). Check it out today at your favorite bookstore. Buy this book now.

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway