2014-04-11 78 views
0

自動成交更新場,我有表 「用戶」 像這樣ADODB,與現場

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'); 

如何如何使用姓氏的值更新字段名字?使用AutoExecute

所以我得到的結果是這樣的:

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

回答

0

除非我誤解你,自動成交似乎並不適合這份工作。如果您需要對錶格中的所有記錄進行一次性轉換,則只需重命名列。

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()); 

乾杯