-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimizetable.php
64 lines (52 loc) · 1.72 KB
/
optimizetable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
// Usage without mysql_list_dbs()
$link = mysql_connect('localhost', 'lpcms', 'lp_1234');
$res = mysql_query("SHOW DATABASES");
while ($row = mysql_fetch_assoc($res)) {
echo $row['Database'] . "<br/>";
}
// Deprecated as of PHP 5.4.0
$link = mysql_connect('localhost', 'lpcms', 'lp_1234');
$db_list = mysql_list_dbs($link);
while ($row = mysql_fetch_object($db_list)) {
echo $row->Database . "<br>";
$db = $row->Database;
//db connection details
//$host = "localhost";
//$username = "root";
//$password = "";
//$db = "wordpress";
//connect to db
//$db_connection = mysql_connect($host, $username, $password) or die("Could not connect to db");
mysql_select_db ($db, $link) or die("Could not connect to table");
//get statuses for tables in db
$sql = "SHOW TABLE STATUS";
$result = mysql_query($sql);
//initialize array
$tables = array();
while($row = mysql_fetch_array($result))
{
// return the size in Kilobytes
$table_size = ($row[ "Data_length" ] + $row[ "Index_length" ]) / 1024;
$tables[$row['Name']] = sprintf("%.2f", $table_size);
//get total size of all tables
$total_size += round($table_size,2);
// optimize tables
$optimise_sql = "OPTIMIZE TABLE {$row['Name']}";
$optimise_result = mysql_query($optimise_sql);
}
//get statuses for tables in db after optimization
$sql = "SHOW TABLE STATUS";
//initialize array
$optimised_tables = array();
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
// return the size in Kilobytes
$table_size = ($row[ "Data_length" ] + $row[ "Index_length" ]) / 1024;
$optimised_tables[$row['Name']] = sprintf("%.2f", $table_size);
//get total size of all tables after optimization
$optimise_total_size += round($table_size,2);
}
}
?>