如何用PHP创建一个新的.MDB文件? [英] How to create a new .MDB file with php?

查看:105
本文介绍了如何用PHP创建一个新的.MDB文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In our in house system, we have csv & xls files generated for users (phpexcel) from our MySQL database, but my boss is asking if is's possible to create a mdb file.

我从未遇到过即时创建新MDB文件的示例,我想知道是否有可能.我不是专家程序员,但是我可以管理是否有人可以指导我如何进行.

I've never come across any sample on creating a new MDB file on the fly, and I wonder if it's possible. I'm not an expert programmer by any chance, but I can manage if anyone could guide me on how I should proceed.

我们正在尝试通过为用户提供mdb而不是他们必须导入到Access的CSV来减少用户的负载/错误.目前,他们必须将100多个csv文件导入Access.

We're trying to reduce user load/mistakes by providing them with mdb instead of CSV that they have to import into Access. Currently they have to import over 100 csv files into Access.

谢谢.

推荐答案

您可以使用COM来访问MDB驱动程序,phpclasses上有一个简单的类可以充当包装器,您可以使用它寻求灵感或直接移植它,完全有可能:

You can use COM for accessing the MDB driver, There is a simple class located at phpclasses that acts as a wrapper for this, you can either use it for inspiration or port it directly, either way it's perfectly possible:

这是课程,在下面提供了源链接:

Here is the class, the source link is provided below:

<?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

这篇关于如何用PHP创建一个新的.MDB文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆