11. 更新記錄(Update)

您可以用傳統的方式:

$sql ="UPDATE t SET name='john', year=28 WHERE year=18";
$conn->Execute($sql);

也可以用以下這種方式:

<?php

// 引入 ADODB
include('adodb/adodb.inc.php');

// 建立連線物件
$conn = &ADONewConnection('mysql');

// 偵錯
$conn->debug=true;

// DSN 四項基本資料設定
$mch="localhost";
$user="piza";
$pwd="ooo123";
$database="test";

// 連接至資料庫 test
$conn->PConnect($mch, $user, $pwd, $database);

// 選擇要更新的那一筆記錄
$sql = "select * from t where year=18";

$rs = $conn->Execute($sql);


// 用一個空陣列來裝要更新的資料
$r = array();

$r['name']='john';
$r['year']=28;

// 用 GetUpdateSQL 函式來製作一個完整的 sql 命令,此 sql 命令放在 $updateSQL 中
$updateSQL = $conn->GetUpdateSQL($rs, $r);

// 執行更新
$conn->Execute($updateSQL);

$conn->Close();
?>

偵錯訊息如下:

-------------------------------------------------------------
(mysql): select * from t where year=18   
-------------------------------------------------------------
(mysql): UPDATE t SET name = 'john', year = 28 WHERE year=18   
-------------------------------------------------------------