我试图弄清楚如何在数据库中定位一个url的所有事件。我想搜索所有表和所有字段。但我不知道从哪里开始,甚至不知道是否有可能。


当前回答

本视频的前30秒展示了如何使用Phpmyadmin的全局搜索功能 这很有效。它将在每个表中搜索字符串。

http://www.vodahost.com/vodatalk/phpmyadmin-setup/62422-search-database-phpmyadmin.html

其他回答

我不记得我在哪里遇到这个脚本,但我一直在使用它与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!';

?>

老帖子我知道,但是对于其他人来说,通过谷歌找到这个,如果你安装了phpmyadmin,它有一个全局搜索功能。

这不是一个优雅的解决方案,但可以通过嵌套循环结构来实现

// 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子句中来加快此过程。

我正在寻找相同的,但找不到它,所以我在PHP中做了一个小脚本,你可以在: http://tequilaphp.wordpress.com/2010/07/05/searching-strings-in-a-database-and-files/

好运! (我删除了一些私人代码,如果我没有在这个过程中破坏它,请告诉我:D)

Scott给出了一个很好的例子,但问题是你为什么要这么做?如果您需要对特定字符串执行查找-替换操作,您还可以尝试对数据库执行mysqldump,在编辑器中执行查找-替换操作,然后重新加载数据库。

也许如果你给出一些你想要达到的目标的背景,其他人可能会提供更好的答案。