Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created May 7, 2013 23:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuaadickerson/5536962 to your computer and use it in GitHub Desktop.
Save joshuaadickerson/5536962 to your computer and use it in GitHub Desktop.
Abstract deletes from the database
<?php
/**
* Delete from one|multiple tables in the database
*
* @param string $identifier = ''
* @param array|string $tables
* @param string $where
* @param array $options = array()
* @param resource $connection = null
*
* @return int the number of affected rows
*/
function smf_db_delete($identifier = '', $tables, $where, array $values = array(), array $options = array(), $connection = null)
{
global $smcFunc;
$tables = is_array($tables) ? $tables : array($tables);
if (empty($smcFunc['db_multi_table_delete']) && count($tables) > 1)
{
$affected_rows = 0;
foreach ($tables as $table)
$affected_rows += smf_db_delete($identifier, $table, $options, $options);
return $affected_rows;
}
$table_string = trim(implode($tables, ', '), ', ');
// Create the update query
$query = '
DELETE FROM ' . $table_string . '
WHERE ' . $options['where'];
$query .= !empty($options['sort']) ? "\n ORDER BY " . $options['sort'] : '';
$query .= !empty($options['limit']) ? "\n LIMIT " . $options['limit'] : '';
$smcFunc['db_query']($identifier, $query, $values, $connection);
return $smcFunc['db_affected_rows']($connection);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment