Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 3.26 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. connect mysql database within CGI
  2. #! /usr/bin/perl -wT
  3.     #@ MAIN_LOGIC
  4.     # todo1.pl - initial version of to-do application
  5.  
  6.     use strict;
  7.     use lib qw(/usr/lib/perl5/);
  8.     use CGI qw(:standard);
  9.     use WebDB;
  10.     use Dumpvalue;
  11.     my $dumper = new Dumpvalue();
  12.     use Carp qw(cluck);
  13.  
  14.     print header(),
  15.         start_html(-title => "To-Do List", -bgcolor => "white"),
  16.         h2("To-Do List");
  17.  
  18.     # Dispatch to proper action based on user selection
  19.     # select STDERR;
  20.     # print("These were the parametersn");
  21.     # my @parameters = param();
  22.     # $dumper->dumpValue(@parameters);
  23.     # select STDOUT;
  24.     my $choice = lc(param("choice"));   # get choice, lowercased
  25.     cluck("choice is $choice");
  26.     if ($choice eq "")                  # initial script invocation
  27.     {
  28.         display_entry_form();
  29.     }
  30.     elsif ($choice eq "submit")
  31.     {
  32.         insert_item(param("content"));
  33.         display_entry_form();
  34.     }
  35.     else
  36.     {
  37.         print p("Logic error, unknown choice: $choice");
  38.     }
  39.  
  40.     print end_html();
  41.     #@ MAIN_LOGIC
  42.  
  43.     exit (0);
  44.  
  45.     # ----------------------------------------------------------------------
  46.  
  47.     # Insert new to-do item if the content is non-empty.
  48.  
  49.     #@ INSERT_ITEM
  50.     sub insert_item
  51.     {
  52.     my $content = shift;
  53.     my $dbh;
  54.  
  55.         $content =~ s/^s+//;   # strip leading whitespace
  56.         $content =~ s/s+$//;   # strip trailing whitespace
  57.         if ($content ne "")     # if content is non-empty, add to table
  58.         {
  59. 58          $dbh = WebDB::connect();
  60. 59          $dbh->do(qq{
  61.                 INSERT INTO todo SET t = NOW(), status = 'open', content = ?
  62.                     }, undef, $content);
  63. 60          $dbh->disconnect();
  64.         }
  65.     }
  66.     #@ INSERT_ITEM
  67.  
  68.     # Display entry form
  69.  
  70.     #@ DISPLAY_ENTRY_FORM
  71.     sub display_entry_form
  72.     {
  73.         print start_form(-action => url()),
  74.             "To-do item:", br(),
  75.             textarea(-name => "content",
  76.                         -value => "",
  77.                         -override => 1,
  78.                         -rows => 3,
  79.                         -columns => 80),
  80.             br(),
  81.             submit(-name => "choice", -value => "Submit"),
  82.             end_form();
  83.     }
  84.     #@ DISPLAY_ENTRY_FORM
  85.        
  86. package WebDB;
  87. #package WebDB for later use;
  88.  
  89. use strict;
  90. use DBI;
  91.  
  92. my $db_name="webdb";
  93. my $user_name="webdev";
  94. my $password="webdevpass";
  95. my $host_name="localhost";
  96. my $dsn = "DBI:mysql:host=$host_name; database=$db_name";
  97.  
  98. #connect to MySQL server, using hardwired name and password
  99.  
  100. sub connect
  101. {
  102.     return (DBI->connect($dsn, "webdev", "webdevpass",
  103.         {PrintError=>0, RaiseError=>1}));
  104. #       || die "Could not connect to database: $DBI::errstr";
  105. }
  106.  
  107. #Connect to MySQL server, using name and password from the current
  108. #user's  ~/.my.cnf option file. The mysql_read_default_file option,
  109. #when added to the DSN, specifies which option file to read.
  110.  
  111. #sub connect_with_option_file
  112. #{
  113. #    #$dsn .= ";mysql_read_default_file=$ENV{HOME}/.my.cnf";
  114. #    $dsn .= ";mysql_read_default_file=/etc/mysql/my.cnf";
  115. #    return (DBI->connect ($dsn, undef, undef, {PrintError=>0, RaiseError=>1}));
  116. #}
  117.  
  118. 1;    #return true
  119.        
  120. [error] [client 127.0.0.1] DBD::mysql::db do failed: No database selected at /usr/lib/cgi-bin/DuBois/todo1.pl line 59., referer: http://localhost/cgi-bin/DuBois/todo1.pl