-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch-replace.php
128 lines (101 loc) · 3.59 KB
/
search-replace.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/*
* Search Whole MySQL Database and replace or do something.
* @author el_ade
*/
// Pick up the form data and assign it to variables
// Search and Replace variables
$search = $_POST['searchFor'];
$replace = $_POST['replaceWith'];
// SAMPLE SEARCH FOR Latin Characters to Latin1 charset
//$search = 'á'; $replace = chr(225);
//$search = 'é'; $replace = chr(233);
//$search = 'í'; $replace = chr(237);
//$search = 'ó'; $replace = chr(243);
//$search = 'ú'; $replace = chr(250);
//$search = 'ü'; $replace = chr(252);
//$search = 'ñ'; $replace = chr(241);
//$search = 'Ñ'; $replace = chr(209);
//$search = '¿'; $replace = chr(191);
//$search = '¡'; $replace = chr(161);
// Query Type: 'search' or 'replace'
//$queryType = $_POST['queryType'];
$queryType = 'replace';
// Database Connection Details
$dbhost = 'localhost'; //addres of your host
$database = 'database'; //Database Name
$dbuser = 'user'; //Database User
$dbpassword = 'password'; //Database User Password
try{
$db = new PDO('mysql:host='.$dbhost.';dbname='.$database, $dbuser, $dbpassword);
} catch (PDOException $e){
echo $e->getMessage();
}
//Prepare the output parameters
$rowHeading = ($queryType=='replace') ?
'Replacing \''.$search.'\' with \''.$replace.'\' in \''.$database."'\n\nSTATUS | ROWS AFFECTED | TABLE/FIELD \n"
: 'Searching for \''.$search.'\' in \''.$database."'\n\nSTATUS | ROWS CONTAINING | TABLE/FIELD \n";
$output = $rowHeading;
$summary = '';
// Get list of tables
$table_sql = 'SHOW TABLES';
$table_q = $db->prepare($table_sql);
$table_q->execute();
$tables_r = $table_q->fetchAll(PDO::FETCH_ASSOC);
foreach ($tables_r as $row){
$table_name = $row['Tables_in_'.strtolower($database)];
$field_sql = 'SHOW FIELDS FROM '.$table_name;
$field_q = $db->prepare($field_sql);
$field_q->execute();
$field_r = $field_q->fetchAll(PDO::FETCH_ASSOC);
foreach ($field_r as $row2){
$field = $row2['Field'];
$type = $row2['Type'];
$key = $row2['Key'];
switch(true) {
// set which column types can be replaced/searched
case stristr ( strtolower ( $type ), 'char' ) :
$typeOK = true;
break;
case stristr ( strtolower ( $type ), 'text' ) :
$typeOK = true;
break;
case stristr ( strtolower ( $type ), 'blob' ) :
$typeOK = true;
break;
case stristr ( strtolower ( $key ), 'pri' ) : // do not replace in index keys
$typeOK = false;
break;
default :
$typeOK = false;
break;
}
if ($typeOK){
// create unique handle for update_sql array
$handle = $table_name.'_'.$field;
if($queryType=='replace') {
$sql[$handle]['sql'] = 'UPDATE '.$table_name.' SET '.$field.' = REPLACE('.$field.',\''.$search.'\',\''.$replace.'\')';
} else {
$sql[$handle]['sql'] = 'SELECT * FROM '.$table_name.' WHERE '.$field.' REGEXP(\''.$search.'\')';
}
// execute SQL
$query = $db->prepare($sql[$handle]['sql']);
$query->execute();
$row_count = $query->rowCount();
// store the output (just in case)
$sql[$handle]['result'] = $query;
$sql[$handle]['affected'] = $row_count;
// Write out Results into $output
$output .= ($query) ? 'OK ' : '-- ';
$output .= ($row_count>0) ? '<strong>'.$row_count.'</strong> ' : '<span style="color:#CCC">'.$row_count.'</span> ';
$fieldName = '`'.$table_name.'`.`'.$field.'`';
$output .= $fieldName;
$output .= "\n";
}
}
}
// write the output out to the page
echo '<pre>';
echo $output."\n";
echo '<pre>';
?>