This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
mysql_update_collation.php
93 lines (77 loc) · 1.88 KB
/
mysql_update_collation.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/*
Author: NewEraCracker
License: Public Domain
*/
/* Stop unwanted access */
if(PHP_SAPI != 'cli'){ die("This script must be ran from CLI.\n"); }
/* Database details */
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = '****';
$dbname = 'wordpress';
/* Collation to change to */
$dbcharset = 'utf8';
$dbcollation = 'utf8_general_ci';
/* Database globals */
$dblink = null;
$dbtype = extension_loaded('mysqli') ? 'mysqli' : (extension_loaded('mysql') ? 'mysql' : null);
/* Function: Connect and select the database */
function db_connect()
{
global $dbhost, $dbuser, $dbpass, $dbname, $dbtype, $dblink;
if( $dbtype == 'mysql' )
{
$dblink = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname, $dblink);
}
else if( $dbtype == 'mysqli' )
{
$dblink = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
}
if( !$dblink )
{
die("Connection to database failed!\n");
}
}
/* Function: Query the database */
function db_query($query)
{
global $dbtype, $dblink;
if( $dbtype == 'mysql' )
{
return mysql_query($query, $dblink);
}
else if( $dbtype == 'mysqli' )
{
return mysqli_query($dblink, $query);
}
}
/* Function: Fetch from the result */
function db_fetch($result)
{
global $dbtype;
if( $dbtype == 'mysql' )
{
return mysql_fetch_array($result);
}
else if( $dbtype == 'mysqli' )
{
return mysqli_fetch_array($result);
}
}
/* Connect */
db_connect();
/* Change default db collation */
db_query("ALTER DATABASE `{$dbname}` DEFAULT CHARACTER SET {$dbcharset} DEFAULT COLLATE {$dbcollation}");
/* Grab the tables */
$show_tables_result = db_query('SHOW TABLES');
while( $tables = db_fetch($show_tables_result) )
{
foreach( $tables as $table )
{
/* Change each table collation */
db_query("ALTER TABLE `{$table}` CONVERT TO CHARACTER SET {$dbcharset} COLLATE {$dbcollation}");
}
}
echo "The collation of your database has been successfully changed!\n";