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

Hi.

I just installed DBI and DBD-Mysql modules on my new computer, but for some reason I cannot connect to Mysql databases through my Perl scripts.

I get an error saying it couldn't connect because it couldn't find the driver at DBI_DSN on line x of my script.

Why is this? Any ideas?

Thanks,
Ralph :)

Replies are listed 'Best First'.
Re: Weird Problem with DBI
by Anonymous Monk on Jul 03, 2001 at 20:00 UTC
    Ok, this is the Perl script:

    #!/usr/bin/perl

    use DBI;

    $dbh = DBI->connect("dbd:mysql:test","username","password") or die "Error!";
    $sth = $dbh->prepare("select * from test");

    @res = $sth->fetchrow_array;

    $all = join('',@res);

    print $all;

    $sth->finish;
    $dbh->disconnect;

    The error I get when connecting running the Perl script is:

    Can't connect(dbd:mysql:test username password), no database driver specified and DBI_DSN env var not set at test.pl line 5.

    Any ideas?

    I really appreciate your help,
    Ralph :)
      perldoc DBD::mysql

      That being said, your DSN passed to connect isn't properly configured. DBD::mysql requires a different syntax in the connect statement requiring hostname and database instead of simply naming the database you wish to use.

      Assuming the mysql-server is on the same box as the script, try: $dbh = DBI->connect("dbi:mysql:database=test;hostname=localhost", "username", "password", {RaiseError => 1});

      The RaiseError is just good use of available attributes of the DBI. Read perldoc DBI for more and what they do.

      ALL HAIL BRAK!!!

Re: Weird Problem with DBI
by Anonymous Monk on Jul 04, 2001 at 06:32 UTC
    Hi.

    I just discovered that the problem was in putting 'dbd:mysql:test', instead of 'dbi:mysql:test'. Still, it's kind of weird how on my other pc it actually worked when using 'dbd'.

    Thanks to all for the great help,
    Ralph :)
Re: Weird Problem with DBI
by Anonymous Monk on Jul 03, 2001 at 18:00 UTC
    Hi.

    I'll post the code later today, cause I'm not at my computer now.

    Ralph :)
Re: Weird Problem with DBI
by Anonymous Monk on Jul 04, 2001 at 00:52 UTC
    Hi.

    I've tried Psycho's solution, but it still doesn't work. Keep returning the same error.

    Any other ideas?

    Ralph :)
Re: Weird Problem with DBI
by maverick (Curate) on Jul 03, 2001 at 09:58 UTC
    Can you paste in the exact error message and your code? it sounds like a typo somewhere, but without seeing your code and the error, I can't say for sure what's happening.

    /\/\averick

Re: Weird Problem with DBI
by busunsl (Vicar) on Jul 03, 2001 at 09:59 UTC
    Yes, lots of ideas! :-)

    But, seriously, we can't help you without seeing what you are doing.
    Post some code and the complete error message and it will be much easier for us to help.