Last active
June 6, 2023 15:32
-
-
Save pkdavies/bad503d9607a4d016af9460647370136 to your computer and use it in GitHub Desktop.
This PHP script will search all tables for a word
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Your search word goes here | |
$search_word = 'wordupdude'; | |
// DB Details | |
$hostname = "mysql.database.azure.com"; | |
$database = "wp_prod"; | |
$username = "wp_prod_user"; | |
$password = "wp_prod_pass"; | |
// Connect to the database | |
$conn = mysqli_connect($servername, $username, $password, $database); | |
if (!$conn) { | |
die('Error connecting to the database: ' . mysqli_connect_error()); | |
} | |
// Fetch all table names in the database | |
$tables = []; | |
$tables_result = mysqli_query($conn, 'SHOW TABLES'); | |
while ($row = mysqli_fetch_row($tables_result)) { | |
$tables[] = $row[0]; | |
} | |
// Iterate through each table and search for the word | |
foreach ($tables as $table) { | |
// Get column names for the table | |
$columns = []; | |
$columns_result = mysqli_query($conn, "SHOW COLUMNS FROM $table"); | |
while ($column_row = mysqli_fetch_assoc($columns_result)) { | |
$columns[] = $column_row['Field']; | |
} | |
// Construct a SELECT query with LIKE conditions for each column | |
$where_conditions = []; | |
foreach ($columns as $column) { | |
$where_conditions[] = "$column LIKE '%$search_word%'"; | |
} | |
$where_clause = implode(' OR ', $where_conditions); | |
$query = "SELECT * FROM $table WHERE $where_clause"; | |
// Execute the query and process the results | |
$results = mysqli_query($conn, $query); | |
if (mysqli_num_rows($results) > 0) { | |
echo "Results found in table: $table\n"; | |
echo "------------------------------------\n"; | |
while ($row = mysqli_fetch_assoc($results)) { | |
// Show the row as a JSON object for simplicity | |
echo json_encode($row, JSON_PRETTY_PRINT) . "\n"; | |
} | |
echo "\n"; | |
} | |
} | |
// Close the database connection | |
mysqli_close($conn); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment