将PHP中的变量传递给查询数据库以进行删除

I am trying to create a cron job in php that deletes disabled users found in a csv file and logs the deleted users to a txt file. Everything works except only the last user in the csv file is deleted. Here is what I have so far:

class purgeInactive extends JApplicationCli
{
    public function doExecute()
    {
        $file     = '../tmp/purge_user.csv';
        $contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $csvRows  = array_map('str_getcsv', $contents);
        $log      = ('../log/purged_users.txt');
        $today    = date('F j, Y, g:ia'); 
        $row      = 1;

        if (($handle = fopen("../tmp/purge_user.csv", "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                $num = count($data);
                $row++;
                for ($c=0; $c < $num; $c++) {
                    file_put_contents($log, PHP_EOL . 'Removed Disabled User(s): ' . $data[$c] . PHP_EOL . '-' . $today . PHP_EOL, FILE_APPEND);

                    $disUsers = implode(',', $data);
                    echo ', ' . $disUsers ; // to test $disUsers output
                } // end for statement
            } // end while statment
        } // end if statement
        fclose($handle);

        $db        = JFactory::getDbo();
        $user      = JFactory::getUser();
        $query     = $db->getQuery(true);
        $userArray = var_export($disUsers,true);

        echo PHP_EOL.'This is to test the output of $userArray'.$userArray;

        $query
            ->select($db->quoteName(array('id')))
            ->from($db->quoteName('#__users'))
            //->delete ($db->quoteName('#__users'))
            //->where(array($db->quoteName('username') . ' IN(' . $userArray) . ')');
            ->where(array($db->quoteName('username') . ' = ' . $userArray));
            //->where($deleteReq);
            $db->setQuery($query);
            //$result = $db->execute();
            $result = $db->query();

        }
    }

    JApplicationCli::getInstance('purgeInactive')->execute();

Any suggestions on how to run each user in the csv file individually? I am about brain dead on this as I have been working on it too long.

Note: I am running Joomla that uses ldap and I use echo for $userArray to check results.

I think instead of ...

->where(array($db->quoteName('username') . ' = ' . $userArray));

You just want

->where(array($db->quoteName('username') . ' IN(' . $userArray) . ')');

That's assuming you want to delete all the rows whose usernames are in $userArray.

If the values in $userArray aren't necessarily SQL-safe, you might also want to do this first:

$userArray = array_map('mysql_real_escape_string', $userArray);

Each row of your csv is iterating this line: $disUsers = implode(',', $data);

This means that all usernames in that row will be saved to $disUsers, then the next iteration will overwrite the old data with the new row's usernames -- this is why only the final row of data is making it down to your delete query.

$data[$c] is what should be pushed into a $disUsers array like this: $disUsers[] = $data[$c];

In your query, you'll need to quote-wrap each array value; this will do that job:

->where($db->qn('username') . ' IN (' . implode(',', array_map(function($username)use($db) {return $db->q($username);}, $disUsers)) . ')')

Here is a post of mine where I discuss writing Joomla queries with IN: https://joomla.stackexchange.com/a/22898/12352