循环遍历数据库中的表,列,字段

时间:2012-11-15 00:50:29

标签: php mysql

我希望我只是忽略了一些东西。现在我知道我在这里没有使用安全表格,但除此之外(请不要打扰我)。

目标:我正在尝试循环访问数据库字段并查找并替换某些字词。

我想我几乎就在那里......首先我得到了表格,然后一旦找到我概述的表格,我就会循环查看表格,它会进入IF。然后我循环遍历字段,一旦找到我追踪的字段(列),它就进入if。

我的问题:现在如何在行中循环?我想我已经在场上设置了自己的发现和替换。我需要它循环遍历该字段的行并查找并替换某些条目。

我的代码:

    foreach ($mstr_keys as $value){

        $old_str = $value;
        $new_str = $mstr_values[$count];

        // connect to mysql
        $con = mysql_connect($dbhost, $dbuser, $dbpass) or die('no connection:'  . mysql_error());
        $db = mysql_select_db($dbname) or die ('cant select db: ' . mysql_error());

        $num_changed = $num_changed_ci = 0;

        // retreive each table
        $sql = "SHOW TABLES FROM `$dbname`";
        $result = mysql_query($sql) or  die ('Could not get tables: ' . mysql_error());

        while ($tbl= mysql_fetch_row($result)) {

            // make sure we are in the set table name
            if ($tbl[0] == $table) {
                echo "\nWorking on Table: {$tbl[0]}... <br />";
                $sql = "SHOW COLUMNS FROM `{$tbl[0]}`";
                $res = mysql_query($sql) or  die ('Could not get columns: ' . mysql_error());
                while ($col = mysql_fetch_array($res)) {

                    // make sure we are in the set column name
                    if ($col[0] == $field) {    
                        //find and replace
                        $sql = "UPDATE `{$tbl[0]}` SET `{$col[0]}` = REPLACE(`{$col[0]}`, '{$old_str}' , '{$new_str}')";

                        $r = mysql_query($sql) or  die ('could not update the field: ' . mysql_error());
                    } else {
                        //do nothing
                    }
                }
            }
        }
        @mysql_close($con);
    }

非常感谢任何和所有帮助。

干杯, 罗布

0 个答案:

没有答案
相关问题