Created
May 7, 2013 23:16
-
-
Save joshuaadickerson/5536962 to your computer and use it in GitHub Desktop.
Abstract deletes from the database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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