Author Topic: Custom MySQL Delete Script - Perl  (Read 9090 times)

DW

  • Administrator
  • Posts: 3787
    • View Profile
    • http://listmailpro.com
Custom MySQL Delete Script - Perl
« on: April 27, 2005, 11:24:47 pm »
Copy and paste the following code into a file named LMdelete.pl

Please read the notes near the top of the file for information about usage.
Code: [Select]
#!/usr/bin/perl

# ListMail Delete User Function
#
# The 2 paramaters are required: list and email
# list can be a single list, ie "1", multiple lists, ie "1,3,2", or "*" for all lists.
#
# The variable below determines whether users are permanently deleted or if their status is set to 'Removed'
# $delete = 1;
$flag = 1;

# example usage:
# require './LMdelete.pl';
# LMdelete('1,2,3','test@listmailpro.com');

# begin function

use DBI;

sub LMdelete {
if(!$_[0]){
print 'You must enter a list number';
return 0;
}
        if(!$_[1]){
                print 'You must enter an email address';
                return 0;
        }

$list = $_[0]; $em = $_[1];

# You -must- set the MySQL connection information below:

$sqlhost = 'localhost';
# $sqldb = 'YOURDATABASE';
# $sqluser = 'YOURUSERNAME';
# $sqlpass = 'YOURPASSWORD';

# End config

# Begin script

# Connect to MySQL
$dbh = '';
$dbh = DBI->connect("DBI:mysql:$sqldb:$sqlhost",$sqluser,$sqlpass);

# parse and put lists in an array
@array = ();
$i = 0;
if(rindex($list,'*') != -1){
# getting all lists
$cmd = "select listnum from lm_lists where 1 order by listnum";
$quer = $dbh->prepare($cmd);
$quer->execute();
$rows = $quer->rows;
while(my ($ln) = $quer->fetchrow_array()){
$array[$i] = $ln;
$i++;
}
} elsif(rindex($list,',') != -1){
@array = split(/,/, $list);
} else {
$array[0] = $list;
}
# loop query

foreach $l(@array){
if($delete){
$cmd = "DELETE FROM lm_users WHERE list = '$l' AND email like '$em';";
} elsif($flag){
$cmd = "UPDATE lm_users SET cnf = '2' WHERE list = '$l' AND email like '$em';";
}
$quer = $dbh->prepare($cmd);
$quer->execute();
}
}
return 1;
Dean Wiebe
ListMailPRO Author & Developer - Help | Support | Hosting