0

我正在尝试执行 PHP/MySQL 查询以选择一组列并清除特定记录的字段:

$sth = $dbh->query("SELECT column_name
                    FROM information_schema.columns
                    WHERE table_name = 'users'
                    AND column_name like '%_abc'");

$rows = $sth->fetchAll();

foreach ($rows as $row){

    $sth = $dbh->prepare("UPDATE users
                          SET $row = NULL
                          WHERE user_id = $user_id");

    $sth->execute();        

}

但是,我收到以下错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: 
Column not found: 1054 Unknown column 'Array' in 'field list'

有任何想法吗?

4

3 回答 3

2

反而 "UPDATE users SET $row = NULL WHERE user_id = $user_id"

尝试"UPDATE users SET {$row['column_name']} = NULL WHERE user_id = $user_id"

于 2012-10-28T13:40:07.433 回答
1

$row的是一个数组。尝试

foreach ($rows as $id => $value {
   ... SET $value = NULL ...
}

你可以看看$row使用:

var_dump($row);
于 2012-10-28T13:31:36.700 回答
0

$row是一个数组。要访问选定行中的特定字段,请使用$row['column_name']

于 2012-10-28T13:29:18.307 回答