in reply to Compare items in DB

You're selecting every record from the table that does not match the number you're looking at. That doesn't tell you whether the number is in the table. I'm guessing you want something like this:

my $sth = $dbh->prepare( 'SELECT count(*) FROM num_table WHERE num = ? +' ); foreach my $n ( @num_from_doc ) { $sth->execute( $n ); my ($exists) = $sth->fetchrow_array; my $not = $exists ? '' : ' not'; print "Number $n is$not in the database.\n"; }

Replies are listed 'Best First'.
Re^2: Compare items in DB
by Anonymous Monk on Mar 05, 2008 at 20:05 UTC
    It worked, just made a simple change to it.
    #my $not = $exists ? '' : ' not'; unless($exists){ print "Number $n is not in the database.<br>\n"; }

    Thank you!!
      Slightly better is print "Number $n is not in the database.<br>\n" unless $exists;
Re^2: Compare items in DB
by Anonymous Monk on Mar 05, 2008 at 20:18 UTC
    Yes it worked, can this be for the reverse?

    my $sth = $dbh->prepare( 'SELECT count(*) FROM num_table WHERE num <> +? +' );

      That will certainly do something, but I think it will be something you don't want.

      Consider a table with these numbers in it: (1, 2, 4, 5). If your number is 2 (which is present), and you select the count of numbers that do not match that, you get 3 (the non-matching numbers are 1, 4, 5). If your number is 3 (which is not present), and you select the same thing, you get 4 (every number there).

      What you want is to tell whether a given number is there or not. What you're suggesting is to count (or list) every number that isn't the given number.