如何用php创建一个新的.MDB文件?

时间:2011-06-28 20:32:09

标签: php ms-access

在我们的内部系统中,我们有csv&从我们的MySQL数据库为用户(phpexcel)生成的xls文件,但我的老板问是否可以创建一个mdb文件。

我从来没有遇到任何关于动态创建新MDB文件的示例,我想知道它是否可行。我不是任何机会的专家程序员,但我可以管理是否有人可以指导我如何继续。

我们尝试通过向用户提供mdb而不是CSV来减少用户负载/错误,他们必须将其导入Access。目前,他们必须将超过100个csv文件导入Access。

谢谢。

2 个答案:

答案 0 :(得分:4)

您可以使用COM来访问MDB驱动程序。有一个位于phpclasses的简单类可以作为它的包装器,您可以使用它来获取灵感或直接移植它,无论哪种方式完全可能:

以下是该类,源链接如下:

<?php    
class mdb 
{ 
  var $RS = 0; 
  var $ADODB = 0; 

  var $RecordsAffected; 

  var $strProvider = 'Provider=Microsoft.Jet.OLEDB.4.0'; 
  var $strMode     = 'Mode=ReadWrite'; 
  var $strPSI      = 'Persist Security Info=False'; 
  var $strDataSource  = ''; 
  var $strConn     = ''; 
  var $strRealPath = ''; 

  var $recordcount = 0; 
  var $ok = false; 


  /** 
  * Constructor needs path to .mdb file 
  * 
  * @param string $dsn = path to *.mdb file 
  * @return boolean success  
  */ 
  function mdb( $dsn='Please enter DataSource!' ) 
  { 
    $this->strRealPath = realpath( $dsn ); 
    if( strlen( $this->strRealPath ) > 0 ) 
    { 
      $this->strDataSource = 'Data Source='.$this->strRealPath; 
      $result = true; 
    } 
    else 
    { 
      echo "<br>mdb::mdb() File not found $dsn<br>"; 
      $result = false; 
    } 

    $this->RecordsAffected = new VARIANT(); 

    $this->open(); 

  } // eof constructor mdb() 


  function open( ) 
  { 
    if( strlen( $this->strRealPath ) > 0 ) 
    { 

      $this->strConn =  
        $this->strProvider.';'. 
        $this->strDataSource.';'. 
        $this->strMode.';'. 
        $this->strPSI; 

      $this->ADODB = new COM( 'ADODB.Connection' ); 

      if( $this->ADODB ) 
      { 
        $this->ADODB->open( $this->strConn ); 

        $result = true; 
      } 
      else 
      { 
        echo '<br>mdb::open() ERROR with ADODB.Connection<br>'.$this->strConn; 
        $result = false; 
      } 
    } 

    $this->ok = $result; 

    return $result; 
  } // eof open() 


  /** 
  * Execute SQL-Statement 
  * @param string $strSQL = sql statement 
  * @param boolean $getrecordcount = true when a record count is wanted 
  */ 
  function execute( $strSQL, $getrecordcount = false ) 
  { 

    $this->RS = $this->ADODB->execute( $strSQL, &$this->RecordsAffected ); 

    if( $getrecordcount == true ) 
    { 

      $this->RS->MoveFirst(); 
      $this->recordcount = 0; 

      # brute force loop 
      while( $this->RS->EOF == false ) 
      { 
        $this->recordcount++; 
        $this->RS->MoveNext(); 
      } 
      $this->RS->MoveFirst(); 

    } 


  } // eof execute() 

  function eof() 
  { 
    return $this->RS->EOF; 
  } // eof eof() 

  function movenext( ) 
  { 
    $this->RS->MoveNext(); 
  } // eof movenext() 

  function movefirst() 
  { 
    $this->RS->MoveFirst(); 
  } // eof movefirst() 

  function close() 
  { 

    @$this->RS->Close(); // Generates a warning when without "@" 
    $this->RS=null; 

    @$this->ADODB->Close(); 
    $this->ADODB=null; 
  } // eof close() 

  function fieldvalue( $fieldname ) 
  { 
    return $this->RS->Fields[$fieldname]->value; 
  } // eof fieldvalue() 

  function fieldname( $fieldnumber ) 
  { 
    return $this->RS->Fields[$fieldnumber]->name; 
  } // eof fieldname() 

  function fieldcount( ) 
  { 
    return $this->RS->Fields->Count; 
  } // eof fieldcount()   

} // eoc mdb 
?>

一个例子:

include 'class_mdb.php'; 

$mdb = new mdb('mymdbfile.mdb'); // your own mdb filename required 
$mdb->execute('select * from table'); // your own table in the mdb file 

# 
# first example: using fieldnames 
#  

while( !$mdb->eof() ) 
{ 
  echo $mdb->fieldvalue('description'); // using your own fields name 
  echo ' = '; 
  echo $mdb->fieldvalue( 1 ); // using the fields fieldnumber 
  echo '<br>'; 
  $mdb->movenext(); 
} 

echo '<br><hr><br>'; 

# 
# Going back to the first recordset for the second example 
# 
$mdb->movefirst(); 

# 
# This works, too: Make each Field an object. The values change 
# when the data pointer advances with movenext(). 
#  
$url = $mdb->RS->Fields(1); 
$bez = $mdb->RS->Fields(2); 
$kat = $mdb->RS->Fields(3); 

while( !$mdb->eof() ) 
{ 
  # works! 
  echo $bez->value; 
  echo ' = '; 
  echo $url->value; 
  echo '<br>'; 
  $mdb->movenext();  
} 

$mdb->close();

来源:http://www.phpclasses.org/package/1700-PHP-Access-Microsoft-Access-mdb-database-files.html

答案 1 :(得分:2)

要创建新的空Microsoft Access数据库,我们可以使用ADOX。

下面的代码在c:\

中创建一个空的Test.mdb文件
    $adox_catalog       = new COM("ADOX.Catalog");

    $adox_catalog->create('Provider = Microsoft.Jet.OLEDB.4.0; Data Source=c:\Test.mdb');

    $adodb_conection = $adox_catalog->activeconnection();