forked from NewEraCracker/php-work
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_mysql_collation.php
More file actions
86 lines (72 loc) · 1.72 KB
/
Copy pathupdate_mysql_collation.php
File metadata and controls
86 lines (72 loc) · 1.72 KB
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
<?php
/*
Author: NewEraCracker
License: Public Domain
*/
/* Database details */
$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = '123456';
$dbname = 'wordpress';
/* Collation to change to */
$dbcharset = 'utf8';
$dbcollation = 'utf8_general_ci';
/* Database globals */
$dblink = null;
$dbtype = ( function_exists('mysqli') ? 'mysqli' : ( function_exists('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);
}
}
/* 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!";
?>