我试图弄清楚如何在数据库中定位一个url的所有事件。我想搜索所有表和所有字段。但我不知道从哪里开始,甚至不知道是否有可能。
当前回答
本视频的前30秒展示了如何使用Phpmyadmin的全局搜索功能 这很有效。它将在每个表中搜索字符串。
http://www.vodahost.com/vodatalk/phpmyadmin-setup/62422-search-database-phpmyadmin.html
其他回答
这不是一个优雅的解决方案,但可以通过嵌套循环结构来实现
// select tables from database and store in an array
// loop through the array
foreach table in database
{
// select columns in the table and store in an array
// loop through the array
foreach column in table
{
// select * from table where column = url
}
}
您可以通过在构建列数组时检查哪些列包含字符串,以及将每个表的所有列合并在一个用逗号分隔的WHERE子句中来加快此过程。
使用MySQL Workbench,您可以从"Database" -> " search Table Data"菜单选项中搜索字符串。
指定LIKE %URL_TO_SEARCH%,并在左侧选择要搜索的所有表。您可以使用“Cntrl + A”选择左侧的整个树,然后取消选择您不关心的对象。
当我们在Wordpress网站上更改域名时,我自己也在寻找这个。没有编程是做不到的,这就是我所做的。
<?php
header("Content-Type: text/plain");
$host = "localhost";
$username = "root";
$password = "";
$database = "mydatabase";
$string_to_replace = 'old.example.com';
$new_string = 'new.example.com';
// Connect to database server
mysql_connect($host, $username, $password);
// Select database
mysql_select_db($database);
// List all tables in database
$sql = "SHOW TABLES FROM ".$database;
$tables_result = mysql_query($sql);
if (!$tables_result) {
echo "Database error, could not list tables\nMySQL error: " . mysql_error();
exit;
}
echo "In these fields '$string_to_replace' have been replaced with '$new_string'\n\n";
while ($table = mysql_fetch_row($tables_result)) {
echo "Table: {$table[0]}\n";
$fields_result = mysql_query("SHOW COLUMNS FROM ".$table[0]);
if (!$fields_result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
if (mysql_num_rows($fields_result) > 0) {
while ($field = mysql_fetch_assoc($fields_result)) {
if (stripos($field['Type'], "VARCHAR") !== false || stripos($field['Type'], "TEXT") !== false) {
echo " ".$field['Field']."\n";
$sql = "UPDATE ".$table[0]." SET ".$field['Field']." = replace(".$field['Field'].", '$string_to_replace', '$new_string')";
mysql_query($sql);
}
}
echo "\n";
}
}
mysql_free_result($tables_result);
?>
希望它能帮助任何在未来遇到这个问题的人:)
您可以通过使用HeidiSQL而不生成Db转储来实现这一点
步骤:
1)在GUI左侧面板中选择需要搜索的数据库。
2)导出>导出数据库为SQL
3)在表格工具窗口中选择“查找文本”选项卡。
4)提供你的字符串进行搜索,然后点击“FIND”。
5)它将列出所有包含我们字符串的表。
6)选择相关度%较高的行。
7)点击“查看结果”
Scott给出了一个很好的例子,但问题是你为什么要这么做?如果您需要对特定字符串执行查找-替换操作,您还可以尝试对数据库执行mysqldump,在编辑器中执行查找-替换操作,然后重新加载数据库。
也许如果你给出一些你想要达到的目标的背景,其他人可能会提供更好的答案。