1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Problem with connecting MySQL using Perl CGI Script (DBI Package)

Discussion in 'Perl' started by sundeep.kumar, Jun 25, 2009.

  1. sundeep.kumar

    sundeep.kumar New Member

    Joined:
    Dec 20, 2007
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Was MIS person once now just a student.
    hi i am using the following code to connect to MySQL database but i am getting this error
    Software error:

    Global symbol "$db" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 16.
    Global symbol "$host" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 17.
    Global symbol "$user" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 18.
    Global symbol "$password" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 19.
    Global symbol "$db" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 21.
    Global symbol "$host" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 21.
    Global symbol "$user" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 22.
    Global symbol "$password" requires explicit package name at C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi line 23.
    Execution of C:/Program Files/Apache Group/Apache2/cgi-bin/data.cgi aborted due to compilation errors.

    code that is been used
    Code:
    #!C:\perl\bin\perl.exe
    
    use DBI;
    use CGI qw(:standard);
    use warnings;
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    
    use strict;
    
    print header;
    
    print start_html("Results Page");
    
    $db="Disease";
    $host="localhost";
    $user="root";
    $password="mysql1234"; 
    #connect to MySQL database
    my $dbh   = DBI->connect ("DBI:mysql:database=$db:host=$host",
                               $user,
                               $password) 
                               or die "Can't connect to database: $DBI::errstr\n";
    
    my $query = new CGI;
    
    my %formdata = $query->Vars;
    
    my $searchkey = $formdata{'star'};
    
    my $sth = $dbh->prepare("select * from  target where Target_type == $searchkey ");
    $sth ->execute();
    
    print '<table border="1">';
    print '<tr><th>Key 1</th><th>key2</th><th>Target Name</th><th>Target Type</th><th>PDB ID</th><th>Modeled Structure</th><th>Uniprot 
    
    link</th><th>Disease Name</th><th>KEGG link</th></th>';
    while (my @row = $sth->fetchrow_array) {
        print 
    
    "<tr><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td><td>$ro
    
    w[8]</td></tr>\n";
    }
    
    print'</table>';
    
    print end_html;
    
    and the HTML script
    HTML:
    <html>
    <head><Title> Query form </title></head>
    <body>
    
    <p> Welcome to database search 
    <p>
    <hr>
    
     <a href ="http://localhost/cgi-bin/test2.pl ">click here </a> to view Target database .
    
    
    <p>
    <hr>
    <p> Searching for a Disease in Target database 
     
    
    <form action="http://localhost/cgi-bin/data.cgi" method="post" > 
       <!--  <p>Enter search Key      : <input type="text" name="skey" /></p>  -->
         <p>Enter Target type   : <input type="text" name="star" /></p> 
         <p><input type="submit" name="Submit" value="Submit Form" /></p> 
       </form> 
    
    </body>
    </html>
    
    please help me out with this
     
    Last edited by a moderator: Jun 25, 2009
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Easiest solution, remove line "use strict;" and run.
     
  3. kvmreddy

    kvmreddy New Member

    Joined:
    Nov 21, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    declare $db $user etc.. as
    my $db
    my $user etc ...
     

Share This Page