in reply to Having issues with DBI

Try like that
my $dbh = DBI->connect("DBI:mysqlPP:host=localhost;database=foo",'bar' +,'baz',{ AutoCommit => 1, RaiseError => 1, PrintErrors=>1}) or die "C +annot connect"; ... some stuff... foreach my $color (@color){ # trim whitespace $color =~ s/^\s+|\s+$//gm; # see if the color already exists in the color table. my $rowCheckQuery = qq{SELECT color_id FROM color W +HERE color=?}; my $sth = $dbh->prepare($rowCheckQuery); $sth->bind($color); $sth->execute() or die $dbh->errstr; }
It helps to you to see warnings and errors

Replies are listed 'Best First'.
Re^2: Having issues with DBI
by zoophagous (Novice) on Dec 11, 2008 at 16:21 UTC
    Thanks all!

    After adding the PrintErrors => 1 I now have an error message to go off of.

    Can't set DBI::db=HASH(0x1ba5f4c)->{PrintErrors}: unrecognised attribute name or invalid value at C:/Perl/site/lib/DBD/mysqlPP.pm line 306

    I suspect the issue is that I am using this to test to see if a value is already in a db. I am expecting empty returns and when I get an empty return I get this behavior. When I insert a test value into the db and hard code the select statement to the test value it works like a champ.

    Is there a better or more correct way to go about testing for existing values in a db?

    Thanks again!
      That error just says that PrintErrors is not a valid setting for DBI. It should be PrintError.

      If you want to solve this, you need to follow some of the advice you're getting. Check the return value from connect(), add strict and warnings. Also, where are you getting your @colors from? I think this is not your real code. Are you reading it from another statement handle, also stored in a variable called $sth?

        Thanks!

        Check the return value from connect(): - will do.

        add strict and warnings: Have had these on the whole time. Always good practice :) Which is why I found it so odd that it was failing silently.

        where are you getting your @colors from?: A big nasty text document. It has a few thousand entries in a semi-normalized format. One of the attributes of each entry is a pipe delimited field which is put into an array. The point though is that @color isn't from another $sth. Though I do have other $sth in the script. Is there a risk from reusing this? Something I should do to ensure this functions as expected?

        Thanks again for the help, I am a n00b when it comes to perl I am grateful for the patience and guidance.
      Well, Sometimes I m checking values like that..
      my $sth = $dbh->prepare(qq{...}); $sth->execute(); if($sth->rows){ #If handler has affected rows }
      OR
      my $sth = $dbh->prepare(qq{...}); $sth->execute(); while(my $row = $sth->fetchrow_hashref()){ #just do something if $sth +can return any records }
      There is more then one way to do (c) ))) Yuo can also try my $hash = $sth->fetchrow_hashref() || undef;