Gossamer Forum
Home : General : Perl Programming :

Pattern Matching

Quote Reply
Pattern Matching
Can anyone please explain to me what is wrong with the following? I'm really running out of hair to pull out here!
Code:
# $sqlstat_isrecordthere = "SELECT id FROM geog";
# $sth0 = $dbh->prepare($sqlstat_isrecordthere);
# $sth0->execute;
# $listofids = $sth0->fetchall_arrayref({});
# $sth0->finish;

# foreach $isidthere (@$listofids) {

# if ($isidthere->{'id'} != "$A::id") {
# &error_html("No such record.");
# &exit(0);
# }
# }



What this snippet of code should do:

Dip into the database.
Select colum id from all records.
See if my $A::id that's passed to the script matches any id found in the database.
If it doesn't, transfer to error subroutine and exit.
If it does find a match, continue onto next sub.

For some reason, the sub keeps telling me that it can not find a match every time. I just can't think of what I'm doing wrong. Anyone with any ideas?

Thanks!!

Wil
Quote Reply
Re: [Wil] Pattern Matching In reply to
Code:
......

my $found = 0;
foreach $isidthere (@$listofids) {
if ($isidthere->{'id'} == "$A::id") {
$found = 1;
last;
}
}

if (!$found) {
&error_html("No such record.");
&exit(0);
}

......



Quote Reply
Re: [sh2sg] Pattern Matching In reply to
Thank you!

Wil
Quote Reply
Re: [Wil] Pattern Matching In reply to
one query is enough.
Code:
...
$sqlstat_isrecordthere = "SELECT id FROM geog where id = '$A::id'";
$sth0 = $dbh->prepare($sqlstat_isrecordthere);
$sth0->execute;
$found = $sth0->fetchrow_array();
$sth0->finish;

if (!$found) {
&error_html("No such record.");
&exit(0);
}

......
Quote Reply
Re: [sh2sg] Pattern Matching In reply to
That doesn't work. Comes up with No records found for every permutation.

Cheers
Quote Reply
Re: [Wil] Pattern Matching In reply to
my $myid = $A::id;
$sqlstat_isrecordthere = "SELECT id FROM geog where id = '$myid' ";

if id is int type, remove the quote from query.
Quote Reply
Re: [sh2sg] Pattern Matching In reply to
But I want to change:

SELECT id FROM geog where id = '$myid' ";

to

SELECT * FROM geog where id = '$myid' ";

so I can get more information later. I could show more fields, that is, not only the ID field.

That was the whole purpose of narrowing this down into one query.

Thanks
Quote Reply
Re: [sh2sg] Pattern Matching In reply to
Done your changes, and still doesn't work.

Cheers
Quote Reply
Re: [Wil] Pattern Matching In reply to
Hi,

This may not be the problem, but you might want to give it a try:

.....
my $sth0 = $dbh->prepare_cached('SELECT * FROM geog WHERE id = ?')
or die "Couldn't prepare statement: " . $dbh->errstr;

$sth0->execute($myid)
or die "Couldn't execute statement: " . $sth0->errstr;

my (@recordfields) = $sth0->fetchrow_array();
.....


--
Matt G
Quote Reply
Re: [Wil] Pattern Matching In reply to
Hi,

Again, don't do the work in perl, if you can do it in SQL.

my $query = "SELECT id FROM geog WHERE id = ?";
my $sth = $dbh->prepare($query);
$sth->execute($A::id);

my ($match) = $sth->fetchrow_array;
if ($match) {
... # got a match
}
else {
... # no match
}

Try to avoid:

->fethcall_arrayref({});

it won't work except on the newer DBI.pms.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Pattern Matching In reply to
Hi Alex

That doesn't seem to want to work for some reason. Here's the code I have now (which does not work):

Code:
$| = 1;

print $query->header;

&get_date;

&connect_to_db;

my $sqlquery = "SELECT * FROM geog WHERE id = $A::id";
my $sth = $dbh->prepare($sqlquery);
$sth->execute;

my ($match) = $sth->fetchrow_array;

if ($match) {

while ($pointer = $sth->fetchrow_hashref) {
$res_id = $pointer->{'id'};
$res_type = $pointer->{'res_type'};
$res_physical = $pointer->{'res_physical'};

. . . . . .
} # end while

} # end if $match

else {
&error_html("No such record.");
&exit(0);
}


Now here's a summary of what I'm trying to do again:

Form with an input box labeled id.
Send the form off to the script above.
Search the database, if we find an ID match, return information on record.
If database does not have an ID match, return error sub.

Any more ideas anyone?

Thanks!

Wil