steampunk333 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, everyone. This may be a noobly question, but for the program I'm trying to create, I need the script to recieve input from an HTML script, use that input(in this case, a name) to search for corresponding information about that person in more than one table, and then return the results to the script, which, in turn, returns it to the web browser. The problem I'm having, however, is that at line 26, the error I'm getting is "too many arguments to connect". Why? What can I do differently to make this script work? The code is all shown below:

#!/usr/bin/perl use DBI; use warnings; my $firstname = $in{name}; my $db = "project_database"; my $host = "localhost"; my $user = "phillip"; my $pw = "steampunk333"; my $source = "DBI:mysql:database = project_database: host = localhost" +; my $contactid; my $dbh; print "Content-type:text/html\n\n"; $dbh = DBI_>connect($source,$user,$pw) or die "Can't connect to database: $DBI::errstr\n"; my $qfirstname = dbh_>quote($firstname); if ($firstname eq "Alan") {$contactid = "Alan W.";} elsif ($firstname eq "Jake") {$contactid = "Jake M.";} elsif ($firstname eq "Jessica") {$contactid = "Jessica S.";} elsif ($firstname eq "Jim") {$contactid = "Jim S.";} else {$contactid = "0"; print "<p><br />Error: No Matches were found for your search. Did you +include capital letters?</p><br /><br />";} if($contactid ne "0") {print "<br /><br /><p>This is the information returned from your quer +y:</p><br /><br />";} my $qcontactid = dbh_>quote($contactid); my $sth = $dbh_>prepare("SELECT lastname, date_first_employed, annual_ +salary FROM employees WHERE firstname = $qfirstname, SELECT * FROM employee_contacts WHERE contact_id = $qcontactid"); $sth_>execute( ); while (($lastname, $date_first_employed, $annual_salary, $date_first_e +mployed, $email_address, $home_phone_number, $emergency_phone_number) = $sth_>f +ethrow_array()) { print "<p>Employee ID: $id<br /><br />Last Name: $lastname<br /><b +r /> Date First Employed: $date_first_employed<br /><br /> Annual Salary: $annual_salary<br /><br />Contact ID: $contactid<br + /><br /> Email Address: $email_address<br /><br />Home Phone Number: $home_ +phone_number<br /><br /> Emergency Phone Number: $emergency_phone_number</p><br /><br />"; } $sth_>finish( ); $dbh_>disconnect( );

Replies are listed 'Best First'.
Re: Too many arguments to connect-help?
by erix (Prior) on May 27, 2014 at 22:18 UTC

    The connectstring should consist of 'dbi:mysql:' (with colons), followed by semi-colon separated parts.

    So,  "dbi:mysql:database = project_database: host = localhost"

    should become  "dbi:mysql:database=project_database;host=localhost;"

    (I think...)

    And  DBI_>connect should be:  DBI->connect (etc)

Re: Too many arguments to connect-help?
by NetWallah (Canon) on May 28, 2014 at 00:57 UTC
    Confirming erix (++) recommendation:

    DBD::mysql confirms the syntax to be:

    $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
    It does not document if "dbi" in lower-case will be accepted.

            What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                  -Larry Wall, 1992

      DBD::mysql [...]

      it does not document if "dbi" in lower-case will be accepted.

      A DBD driver only prescribes the dsn-part after 'dbi:drivername:', and the DBI docs do document it; in fact, it actually does not document that uppercase DBI is accepted :-)

      DBI connect

      It says:

      The $data_source value must begin with "dbi:driver_name:". The driver_name specifies the driver that will be used to make the connection. (Letter case is significant.)
      update: (@NetWallah's below reply), yes I quite agree.
        So - the documented examples between the DBD and DBI modules conflict.

        However, the code in DBI.pm resolves this conflict - it says:

        sub connect { .... # extract dbi:driver prefix from $dsn into $1 $dsn =~ s/^dbi:(\w*?)(?:\((.*?)\))?://i or '' =~ /()/; # ensure $1 etc are empty if match fails
        so - it will accept EITHER case for the "DBI" part.

        The part about "(Letter case is significant.)" applies to the DBD driver name, not the the "DBI" or "dbi" - it will even accept "DbI".

                What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
                      -Larry Wall, 1992