adodb,AutoExecute更新字段与字段

时间:2014-04-11 09:13:49

标签: database field updates adodb autoexec

我有桌子"用户"像这样

id  | firstname | lastname
==========================
1   | Ujang     | ahmad
2   | Jajat     | sudrajat

并拥有数据:

$record = array('firstname'=>'some value', 'lastname'=>'some value');
$table  = "users";

并像这样处理更新:

$exc= $conn->AutoExecute($table, $record, 'UPDATE', 'id = 1');

如何使用lastname的值更新字段firstname使用AutoExecute

所以我得到了这样的结果:

id  | firstname | lastname
==========================
1   | ahmad     | Ujang
2   | sudrajat  | Jajat

1 个答案:

答案 0 :(得分:0)

除非我误解你,否则AutoExecute似乎不适合这份工作。如果您需要对表中的所有记录进行一次性转换,我只需重命名列。

ALTER TABLE `users`
CHANGE COLUMN `lastname` `firstname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `id`,
CHANGE COLUMN `firstname` `lastname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `firstname`;

或者在PHP / ADODB中:

$sql = "ALTER TABLE `users`
  CHANGE COLUMN `lastname` `firstname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `id`,
  CHANGE COLUMN `firstname` `lastname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `firstname`;";
if (($result = $conn->Execute($sql)) === false)
  exit($sql.'<br />'.$conn->errorMsg());

如果您需要定位特定记录,则可以使用临时变量。

$sql = "UPDATE users
  SET firstname=(@temp:=firstname), firstname = lastname, lastname = @temp
  WHERE id=2";
if (($result = $conn->Execute($sql)) === false)
  exit($sql.'<br />'.$conn->errorMsg());

干杯