網頁

2015年2月24日

[php] 如何用PHP程式讀取Microsoft Access資料庫(.mdb)

PHP程式範例:
<?php
//mdb檔案路徑
$mdb_path = 'C:\temp\MyDB.mdb';

//New COM元件
$c = new COM("ADODB.Connection");
$c -> open("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=".$mdb_path.";");

$sql = "select userid, username from userinfo";
$r = $c->execute($sql);
$fields_count = $r->fields->count();
$emp = array();
if ($fields_count){
 while (!$r->EOF) {
  for($n = 0; $n<$fields_count; $n++) {
   $k = $r->fields($n);
   switch ($k->name){
    case 'userid':
     $userid = $k->value;
     break;
    case 'username':
     $username = $k->value;
     break;
   }
  }
  //資料傳入陣列
  $emp[$userid] = $username;

  $r->MoveNext();
 }
}
$r->close();

print_r($emp);
?>

輸出結果:
Array ( [9997] => 王小明 [9998] => 張二牛 [9999] => 伍三狼 )