<?
/*
### D    A    T    A    B    A    S    E        O    P    T    I    M    I    Z    E    R ###

#############################################################
###### Made By Recruiting Grounds                          ######
###### Nino Skopac, the RG Leader, Owner, Founder       ######
###### Creation time: ~30 minutes                       ######
###### SUPPORT : http://forum-grounds.com               ######
###### RG URL : http://recgr.com                       ######
###### INITIAL RELEASE : http://dev.recgr.com           ######
###### PM GROUNDS : http://pmgrounds.com               ######
#############################################################

-------------------------------------------------------------
This application will optimize your database.
I recommend using it once per week.
-------------------------------------------------------------

##############    ARGUMENTS / HOW TO USE IT    ##############
1) $db = the database you want to optimize
2) $connectToo = defaults to <i>false</i>. change it (to anything) if you want the script to connects to your DB firstly
3) $username = required if you used second argument - the username of your DB server
4) $password = required if you used second argument - the password of your DB server
5) $host = you can leave it as-is if your server is <i>localhost</i>; otherwise change it

Example (all arguments)
<i>optimizeDatabase('site', true, 'nino', 'mypassword', 'localhost');</i>

That will optimize table <i>site</i> but it will firstly connect to DB server <i>localhost</i> using username <i>nino</i>, password <i>mypassword</i>.

Example (two arguments)
<i>optimizeDatabase('site', true);</i>

This will probably work on your localhost if you didn't bother to configure it.
It will optimize database <i>site</i> and it WILL connect to DB firstly.
The username, password and server location will be default ('root', '', 'localhost')

*/
function optimizeDatabase($db$connectToo false$username 'root'$password ''$host 'localhost') {

if (
$connectToo !== false) {
@
mysql_connect($host$username$password) or die("Cannot connect to $host using username <strong>$username</strong>.");
@
mysql_select_db($db) or die("Cannot pick database $db using username <strong>$username</strong>.");
}


$sqlGetTables "SHOW TABLES FROM $db";
$qGetTables = @mysql_query($sqlGetTables) or die ("Cannot execute that query: ".mysql_error());

$num 0;
$tables = array();
while (
$f mysql_fetch_row($qGetTables)) {

$tables[$num] = $f[0];

$num++;
}

$parse implode('`, `'$tables);
$list '`'.$parse.'`';


$optimize = @mysql_query("OPTIMIZE TABLE $list");

if (
$optimize) {
echo 
"Your database $db has been successfully optimized!";
} else {
echo 
"I couldn't optimize your tables! Error: ".mysql_error();
}

@
mysql_free_result($qGetTables);

if (
$connectToo !== false) {
@
mysql_close();
}
}
?>
1