Skip to content

Instantly share code, notes, and snippets.

@josefglatz
Created November 12, 2014 17:34
Show Gist options
  • Save josefglatz/9d8523ab027883becbf9 to your computer and use it in GitHub Desktop.
Save josefglatz/9d8523ab027883becbf9 to your computer and use it in GitHub Desktop.
<?php
/****************************************************/
// db_delete_records.php //
// Author: Christian Wolfram <[email protected]> //
// Date: 12-11-2014 //
// //
// Deletes all records in the database that //
// are marked as deleted. //
// //
// Run from root directory of your TYPO3 //
// installation. //
/***************************************************/
// Set to TRUE to generate an enormous amount of debug output with
// analysis of table structure.
define('DEBUG', FALSE);
// Set to FALSE to really delete records in database
define('SIMULATE', TRUE);
require_once ('typo3conf/localconf.php');
echo str_repeat(' ', 256);
?>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
.normal {
color: black;
}
.okay {
color: green;
}
.label {
color: blue;
}
.error {
color: red;
}
</style>
</head>
<body>
<?php
if(SIMULATE) {
echo '<h1 class="error">Löschen wird simuliert</h1>';
} else {
echo '<h1 class="okay">Daten werden gelöscht</h1>';
}
$db = mysql_connect($typo_db_host, $typo_db_username, $typo_db_password, TRUE);
if (!is_resource($db)) {
die('Could not connect to db!: ' . mysql_error());
}
if (mysql_select_db($typo_db, $db) === FALSE) {
die('Could not select database!: ' . mysql_error());
}
$tables = array();
// Collect table names
$sql = 'SHOW TABLES;';
$db_res = mysql_query($sql, $db);
if (!is_resource($db_res)) {
die ('Could not get query result!: ' . mysql_error() . "\n" . $sql);
}
while ($row = mysql_fetch_array($db_res, MYSQL_NUM)) {
$tables[] = $row[0];
}
if (count($tables) > 0) {
// process each table
foreach ($tables AS $table) {
$sql = 'SHOW FULL COLUMNS FROM `' . $table . '`;';
$db_res = mysql_query($sql, $db);
if (!is_resource($db_res)) {
die ('Could not get table data!: ' . mysql_error() . "\n" . $sql);
}
while ($row = mysql_fetch_assoc($db_res)) {
if ($row['Field'] == 'deleted') {
if (DEBUG) {
echo '<br>column: ' . $row['Field'] . ' of table ' . $table;
echo '<pre>'; var_dump($row); echo '</pre>';
}
// count all as deleted marked records
$sql2 = 'SELECT COUNT(*) FROM ' . $table . ' WHERE deleted=1';
$db_res2 = mysql_query($sql2);
$count = mysql_fetch_row($db_res2);
mysql_free_result($db_res2);
if(!SIMULATE && $count[0] > 0) {
$sql3 = 'DELETE FROM ' . $table . ' WHERE deleted=1';
$db_res3 = mysql_query($sql3, $db);
$count[0] = mysql_affected_rows();
mysql_free_result($db_res3);
}
echo '<span class="label">' . $table . ':</span> ' . $count[0] . ' deleted<br>';
break;
}
}
mysql_free_result($db_res);
}
}
echo '<div><br>finished delete records in all tables</div>';
mysql_close($db);
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment