Gossamer Forum
Home : General : Perl Programming :

Finding someone in SQL...

Quote Reply
Finding someone in SQL...
Hi. Could someone please give me some sample code on how to search through a SQL table (i.e. username) and then report a true value if it exists?

I have been playing with this idea, and found a bit of documentation on it, but it wasnt what i wanted to do.

Thanks

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [AndyNewby] Finding someone in SQL... In reply to
I would pass the username you are checking to the script and then run a simple check on the database like so.

Code:
$sth = $dbh->prepare("SELECT username FROM $DB_MYSQL_NAME WHERE username = '$username'");
$sth->execute;
$ref = $sth->fetchrow_hashref();
$sth->finish;

if (!$ref) {
die("$username not found in database.");
}

An alternative idea would be to grab all rows from the database and and then check to see if username exists. The first example above is faster IMO.

Cheers

- wil
Quote Reply
Re: [AndyNewby] Finding someone in SQL... In reply to
Andy, I don't believe you couldn't find information on this at mysql.com.

I mean, jeez, all you needed to do was search for SELECT and go to:

http://www.mysql.com/doc/S/E/SELECT.html

...and it gives you about 50 examples.

Last edited by:

PaulW: Nov 26, 2001, 4:01 AM