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

Am having a problem with selecting users emails

this is my DB

DB USERS ID USERID EMAIL 1 XD4555 JOHNE@DEO.COM 2 JJKKKK JANE@DEO.COM 3 JJKKKK JANE21@DEO.COM FUSERS ID USERID USERID_IM 1 XD4555 JJKKKK 2 JJKKKK TYYUPPO
my try
my $AID = "XD4555"; my $query = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID WHERE (m.USERID_IM = ?)"); $query->execute($AID); my $user1 = $query->fetchall_arrayref(); my $query_other = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID_IM WHERE (m.USERID = ?)"); $query_other->execute($AID); my $user2 = $query_other->fetchall_arrayref(); my $TotalEmails = $user1 + $user2; foreach my $em ( $TotalEmails ) { my $rez = join( ",", $em); print "$rez\n"; }

now this is the results am getting, its random generated

189822522692296

this is the output i expect

email@email.com, email@email.com

Replies are listed 'Best First'.
Re: Selecting DB
by Corion (Patriarch) on Jul 08, 2023 at 12:57 UTC

    This likely is not what you want:

        my $TotalEmails = $user1 + $user2;

    Most likely you wanted

    my @TotalEmails = (@$user1, @$user2);

    If you allow Perl to tell you when you're doing non-sensical stuff, please add the following two lines to the top of your script:

    use strict; use warnings;

      this is what i get

      ARRAY(0x56295a1b7458) ARRAY(0x56295a2c1878)

      when i try this

      my @TotalEmails = (@$user1, @$user2);
Re: Selecting DB
by cavac (Prior) on Jul 10, 2023 at 12:19 UTC
    Note: Pseudo-Code in this answer is written from memory and is untested. Didn't have time to build it to scale or to paint it.

    Why code the stuff twice? you can put the database stuff into a function and return the result. Then run a foreach loop to request everything:

    my @userids = ('XD4555', 'JJKKKK'); my @mails; foreach my $userid (@userids) { push @mails, getMails($dbh, $userid); } sub getMails { my ($dbh, $userid) = @_; my $sth = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERI +D WHERE m.USERID_IM = ?"); $sth->execute($userid); my $mails = $sth->fetchall_arrayref(); return @{$mails}; # Caller wants an array, so deref }

    Of course, with most databases you can use the IN keywords to look for multiple matches at once. This is generally faster, since you have less calls to the database AND the statement optimizer only has to run once:

    my @userids = ('XD4555', 'JJKKKK'); my $sth = $dbh->prepare("SELECT snd.EMAIL FROM FUSERS as m JOIN USERS as snd ON snd.USERID = m.USERID WHERE m.USERID_IM IN (?)"); $sth->execute(\@userids); my $mails = $sth->fetchall_arrayref();

    It's also important here that you completely fail to error-check the database calls. This will lead to all sorts of problems and wrong results. And SELECT statements usually require a call to finish(). Try something like this:

    use strict; use warnings; use Carp; ... my $dbh = DBI->connect(...parameters here...) or croak("$!"); ... my $sth = $dbh->prepare(...statement here...) or croak($dbh->errstr); $sth->execute(...args here...) or croak($dbh->errstr); my $mails = $query->fetchall_arrayref(); $sth->finish;

    Also, it would be a VERY good idea to use proper transactions (not that autocommit BS), especially if you also INSERT/UPDATE/DELETE.

    if($dbh->do(...somestuff...) && $dbh->do(...otherstuff...)) { $dbh->commit; } else { print STDERR "Database error: ", $dbh->errstr, "\n"; $dbh->rollback; }

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Re: Selecting DB
by Anonymous Monk on Jul 08, 2023 at 12:59 UTC
    this is the output i expect email@email.com, email@email.com
    How? That isn't in your source data.
A reply falls below the community's threshold of quality. You may see it by logging in.