我试图弄清楚如何在数据库中定位一个url的所有事件。我想搜索所有表和所有字段。但我不知道从哪里开始,甚至不知道是否有可能。
当前回答
当我们在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);
?>
希望它能帮助任何在未来遇到这个问题的人:)
其他回答
当我们在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);
?>
希望它能帮助任何在未来遇到这个问题的人:)
一个简单的解决方案是这样做的:
mysqldump -u myuser --no-create-info --extended-insert=FALSE databasename | grep -i "<search string>"
我正在寻找相同的,但找不到它,所以我在PHP中做了一个小脚本,你可以在: http://tequilaphp.wordpress.com/2010/07/05/searching-strings-in-a-database-and-files/
好运! (我删除了一些私人代码,如果我没有在这个过程中破坏它,请告诉我:D)
这里有两个简单的代码。首先执行mysqldump:
mysqldump -uUSERNAME -p DATABASE_NAME > database-dump.sql
然后grep sqldump文件:
grep -i "Search string" database-dump.sql
也可以找到/替换并重新导入到数据库中。
我不记得我在哪里遇到这个脚本,但我一直在使用它与XCloner移动我的WP多站点。
<?php
// Setup the associative array for replacing the old string with new string
$replace_array = array( 'FIND' => 'REPLACE', 'FIND' => 'REPLACE');
$mysql_link = mysql_connect( 'localhost', 'USERNAME', 'PASSWORD' );
if( ! $mysql_link) {
die( 'Could not connect: ' . mysql_error() );
}
$mysql_db = mysql_select_db( 'DATABASE', $mysql_link );
if(! $mysql_db ) {
die( 'Can\'t select database: ' . mysql_error() );
}
// Traverse all tables
$tables_query = 'SHOW TABLES';
$tables_result = mysql_query( $tables_query );
while( $tables_rows = mysql_fetch_row( $tables_result ) ) {
foreach( $tables_rows as $table ) {
// Traverse all columns
$columns_query = 'SHOW COLUMNS FROM ' . $table;
$columns_result = mysql_query( $columns_query );
while( $columns_row = mysql_fetch_assoc( $columns_result ) ) {
$column = $columns_row['Field'];
$type = $columns_row['Type'];
// Process only text-based columns
if( strpos( $type, 'char' ) !== false || strpos( $type, 'text' ) !== false ) {
// Process all replacements for the specific column
foreach( $replace_array as $old_string => $new_string ) {
$replace_query = 'UPDATE ' . $table .
' SET ' . $column . ' = REPLACE(' . $column .
', \'' . $old_string . '\', \'' . $new_string . '\')';
mysql_query( $replace_query );
}
}
}
}
}
mysql_free_result( $columns_result );
mysql_free_result( $tables_result );
mysql_close( $mysql_link );
echo 'Done!';
?>
推荐文章
- MySQL选择列不为空的位置
- 哪个更快:多个单个INSERT还是一个多行INSERT?
- 搜索mysql数据库中出现的所有字符串
- 如何使用MySQL DECIMAL?
- 警告用户/local/mysql/data目录不属于mysql用户
- 添加一个复合主键
- 无法添加或更新子行:外键约束失败
- 如何从本地机器mysqldump远程数据库
- 如何正确地创建复合主键- MYSQL
- 仅在Datetime列上按日期分组
- 在MySQL数据库中存储货币值的最佳数据类型
- 如何自定义'显示进程列表'在mysql?
- MySQL删除一些外键
- 用MySQL LEFT JOIN删除行
- MySQL - length() vs char_length()