【问题标题】:How to search for a string in a mysql database in php如何在php中的mysql数据库中搜索字符串
【发布时间】:2014-12-17 21:18:12
【问题描述】:

我正在尝试检索整个数据库中的字符串,但没有提及表和列的名称,但我的 PHP 代码显示错误

警告:mysql_fetch_row() 期望参数 1 是资源,布尔值

谁能帮我看看哪里出错了?

<?php

 // Setup the associative array for replacing the old string with new string
    $replace_array = "test";
    $HHHH;
    $ffff;
$mysql_link = mysql_connect( 'localhost:3306', 'tribhuvan', '123456' );
    if( ! $mysql_link) {
        die( 'Could not connect: ' . mysql_error() );
    }

    $mysql_db = mysql_select_db( 'a_t', $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                    
                    $query = 'SELECT * From ' . $table . 
                            ' WHERE ' .  $column . ' = "'.$replace_array.'"';
                $HHHH = mysql_query($query);
                $ffff = mysql_fetch_row($HHHH);


                }
            }
        }
    }
    while( $queryValues = $ffff ) {
        foreach( $queryValues as $Values ) {
            echo $Values."<br/>";
            }}  

 mysql_free_result( $columns_result );
    mysql_free_result( $tables_result );
    mysql_close( $mysql_link );

    echo 'Done!';


?>

【问题讨论】:

标签: php mysql


【解决方案1】:

$HHHH 中的结果显然是无效的。如果您回显 $query,您可以看到会发生什么。可能有些东西不在那里。但是因为我们不知道 $table 的值,所以我不知道可能出了什么问题。

忠告:mysql 有点过时了。当您将 MySQL 与 PHP 一起使用时,建议是 MySQLi:阅读此处:http://php.net/manual/en/function.mysql-fetch-row.php

【讨论】:

  • 看看我上面的评论。
猜你喜欢
  • 2015-01-30
  • 1970-01-01
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 2014-07-28
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多