PDA

View Full Version : How do I check whether a record exists?


ilya
03-16-2000, 12:31 AM
I have a table (users) with fields (FirstName,LastName,Email)
Say, I search for "test". How do i check whether a FirstName field with that value exists or not?
I need this in perl code, please.

Dan Kaplan
03-16-2000, 01:34 AM
I don't think I've ever helped someone with actual Perl/MySQL (assuming you're using MySQL?) code before, and I'm far from knowledgeable in that arena, so keep in mind that there's a good chance this isn't right or optimal ;)[nbsp][nbsp]:

SELECT *
FROM Users
WHERE Users.FirstName = 'value'

I think that will at least get you in the ballpark.[nbsp][nbsp]The rest might require some gentle surgical manipulation

Dr. Who

pier
03-16-2000, 03:24 PM
so keep in mind that there's a good chance this isn't right or optimal:
SELECT * FROM Users WHERE Users.FirstName = 'value'

Indeed :P

a[nbsp][nbsp]'select * from Users' requires an additional query (processed on the database), namely a 'describe Users' (or alike). A better thing to do is a
select count(firstname) from Users where..
or (when using oracle)
select count(1) from Users where...

Note that you'll have to read the countvalue now, because this will always return a value.

Pier

ilya
03-16-2000, 08:42 PM
When I use "SELECT * FROM users WHERE FirstName='$in{firrstname}";
[nbsp]if the record doesn't exist, it won't print out anything.
I thought maybe if i use this:
$SQL="SELECT * blahblahblah";
$sth=$dbh->prepare($SQL); # previously conected to DB in dbh variable
$sth->execute;
if (!$sth) {
print "Record not found";
}

will that work??

ilya
03-19-2000, 11:18 PM
Can anyone please help??

urban
03-21-2000, 01:08 AM
This might work for you...


$sql = "select * from users where name = 'ilya'";
$sth=$dbh->prepare($sql); # previously conected to DB in dbh variable
$sth->execute;
if ($sth->rows() == 0) {
[nbsp][nbsp]print "\nNo Matching Records.\n";
}[nbsp][nbsp][nbsp][nbsp]
[nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp][nbsp]