Gossamer Forum
Home : General : Perl Programming :

can this be done in perl with mysql

Quote Reply
can this be done in perl with mysql
Hello,
Im curious if this is possible to write in perl to interact with a mysql database.

We have someone log into our system, their info is placed in a mysql table named loggedin, whenever someone hits on the who's on script it checks the loggedin table, finds any old entries and removes them, but let's say theyve been sitting at the menu.cgi for a long time and our system logs them out, now what I would like to do is to have a statement in perl that would insert the user into the loggedin table if they are no longer present or update them if they are still in the table, we have the following for updating them,

Code:
sub updatewhoson {
$query = "UPDATE lastlogin SET location = 'Browsing All' where Name = '$member'";
$dbh->do($query);
$query = "UPDATE lastlogin SET ttime = unix_timestamp() where Name = '$member'";
$dbh->do($query);
While I could just put in the following statement in the script:
Code:
sub updatewhoson {
$query = "INSERT into lastlogin SET location = 'Browsing All' where Name = '$member'";
$dbh->do($query);
$query = "INSERT into lastlogin SET ttime = unix_timestamp() where Name = '$member'";
$dbh->do($query);
What would happen is that since they are already listed, it would write to the server error log which I want to avoid, I'd like it to check if they are in the table, and update them, if they are not, then insert them.

Anybody know a convienient way of doing this?

Thanks
Harrison


"I've got if's pretty good, but that's about it"
Quote Reply
Re: can this be done in perl with mysql In reply to
$sth = $dbh->prepare("SELECT * FROM lastlogin WHERE Name = '$member'");
$sth->execute();
if (! $sth->rows) {
# this user is new..
$dbh->do("INSERT INTO lastlogin SET Name = '$member', location = 'Browsing All', ttime = unix_timestamp()");
}
else {
# this user is already in table..
$dbh->do("UPDATE INTO lastlogin SET location = 'Browsing All', ttime = unix_timestamp() WHERE Name = '$member'");
}

something like that..

Jerry Su
email@jerrysu.com
http://www.jerrysu.com/