Skip to content

Instantly share code, notes, and snippets.

@javierav
Created October 31, 2010 22:03
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 javierav/657221 to your computer and use it in GitHub Desktop.
Save javierav/657221 to your computer and use it in GitHub Desktop.
Elimina tablas MySQL en base a un prefijo dado
<?php
/**
* Script que elimina de una base de datos todas las tablas que empiezan
* por el prefijo dado en $prefix
*
* Javier Aranda <internet at javierav dot com> - Aula de Software Libre
* Universidad de Córdoba
**/
//
/// Configuración
//
$hostname = 'localhost';
$username = 'user';
$password = 'pass';
$database = 'db';
$prefix = 'forum_';
//
/// No tocar a partir de aquí
//
$link = mysql_connect($hostname, $username, $password);
if( ! $link)
{
die('Falló la conexión al servidor');
}
if ( ! mysql_select_db($database, $link) )
{
die('Falló la selección de la base de datos');
}
// Consulta SQL para obtener las tablas
$sql = 'SHOW TABLES FROM '.$database." LIKE '".$prefix."%'";
$result = mysql_query($sql, $link);
if( ! $result OR mysql_num_rows($result) == 0)
{
die('No se ha obtenido ningún resultado para la consulta: '.$sql);
}
while ($row = mysql_fetch_row($result)) {
$tables .= $row[0].", ";
}
//Eliminar la coma final
$tables = substr($tables, 0, strlen($tables) - 2);
if( isset($_GET['confirm']) AND $_GET['confirm'] == 1)
{
$sql = 'DROP TABLE '.$tables;
if( mysql_query($sql, $link) )
{
echo 'La operación se ha sucedido con éxito';
}
else
{
echo 'Las tablas no han podido ser borradas';
}
}
// Mostrar formulario de confirmación
else
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Borrador de tablas</title></head><body>
<?php
echo 'Lista de tablas que serán borradas: <br /><br />';
echo $tables;
echo '<br /><br />';
?>
<form method="get" action="<?php echo basename(__FILE__); ?>" name="confirm">
<table style="text-align: left; width: 100%; margin-left: auto; margin-right: auto;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top; text-align: center;">¿Desea realmente eliminar las tablas?<br>
<button value="1" name="confirm">Eliminar</button><br>
<br>
</td>
</tr>
</tbody>
</table>
<br>
</form>
</body></html>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment