-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathphp-search-all-database.php
More file actions
166 lines (126 loc) · 4.25 KB
/
php-search-all-database.php
File metadata and controls
166 lines (126 loc) · 4.25 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
$search_keyword = 'KEYWORD'; // Keyword to search.
// Table => searchable columns mapping.
$table_associative_array = [
'TABLE_NAME_1' => ['column_name_a', 'column_name_b'],
'TABLE_NAME_2' => ['column_name_a', 'column_name_b'],
];
// Optional row-id column (set null to hide)
$row_identifier_column = 'id';
php_search_all_database($search_keyword, $table_associative_array, $row_identifier_column);
/**
* Search keyword across multiple tables/columns.
*/
function php_search_all_database($search_keyword, $table_associative_array, $row_identifier_column = 'id')
{
$conn = mysqli_connect('DATABASE HOST NAME', 'DATABASE USERNAME', 'DATABASE PASSWORD', 'DATABASE NAME');
if (!$conn) {
echo 'DB Connection Failed: ' . mysqli_connect_error();
return;
}
if (trim($search_keyword) === '') {
echo 'Keyword cannot be empty.';
mysqli_close($conn);
return;
}
if (empty($table_associative_array)) {
echo 'No tables configured.';
mysqli_close($conn);
return;
}
echo '<b>Keyword:</b> ' . e($search_keyword) . '<br>';
echo '<b>Tables:</b> ' . e(implode(', ', array_keys($table_associative_array))) . '<br><hr>';
$total_matches = 0;
$like_value = '%' . $search_keyword . '%';
foreach ($table_associative_array as $table_name => $columns) {
$safe_table = sanitize_identifier($table_name);
$safe_columns = sanitize_identifiers($columns);
if ($safe_table === null || empty($safe_columns)) {
echo 'Invalid table/columns: ' . e($table_name) . '<br><hr>';
continue;
}
// Build WHERE clause
$where = [];
$types = '';
$params = [];
foreach ($safe_columns as $col) {
$where[] = "`$col` LIKE ?";
$types .= 's';
$params[] = $like_value;
}
$sql = "SELECT * FROM `$safe_table` WHERE " . implode(' OR ', $where);
$stmt = mysqli_prepare($conn, $sql);
if (!$stmt) {
echo 'Prepare failed: ' . e($table_name) . '<br><hr>';
continue;
}
mysqli_stmt_bind_param($stmt, $types, ...$params);
if (!mysqli_stmt_execute($stmt)) {
echo 'Execution failed: ' . e($table_name) . '<br><hr>';
mysqli_stmt_close($stmt);
continue;
}
$result = mysqli_stmt_get_result($stmt);
echo '<h4>Table: ' . e($table_name) . '</h4>';
if (!$result || mysqli_num_rows($result) === 0) {
echo 'No matches found.<br><hr>';
mysqli_stmt_close($stmt);
continue;
}
while ($row = mysqli_fetch_assoc($result)) {
$matched = false;
$html = '';
foreach ($safe_columns as $col) {
if (!isset($row[$col])) continue;
$value = (string)$row[$col];
if (stripos($value, $search_keyword) !== false) {
$matched = true;
$total_matches++;
$html .= '<li><b>Column:</b> ' . e($col) . '</li>';
$html .= '<li><b>Value:</b> ' . e($value) . '</li>';
}
}
if ($matched) {
echo '<ul>';
if ($row_identifier_column !== null && isset($row[$row_identifier_column])) {
echo '<li><b>Row:</b> ' . e($row[$row_identifier_column]) . '</li>';
}
echo $html;
echo '</ul>';
}
}
mysqli_free_result($result);
mysqli_stmt_close($stmt);
echo '<hr>';
}
echo '<b>Total Matches:</b> ' . $total_matches;
mysqli_close($conn);
}
/**
* Validate SQL identifier (table/column)
*/
function sanitize_identifier($name)
{
return (is_string($name) && preg_match('/^[A-Za-z0-9_]+$/', $name)) ? $name : null;
}
/**
* Validate multiple identifiers
*/
function sanitize_identifiers($names)
{
$valid = [];
foreach ($names as $name) {
$clean = sanitize_identifier($name);
if ($clean !== null) {
$valid[$clean] = $clean;
}
}
return array_values($valid);
}
/**
* Safe HTML output
*/
function e($value)
{
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
}