How to create a new .MDB file with php?

前端 未结 2 1457
刺人心
刺人心 2021-01-03 09:40

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.

2条回答
  •  借酒劲吻你
    2021-01-03 09:54

    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:

    strRealPath = realpath( $dsn ); 
        if( strlen( $this->strRealPath ) > 0 ) 
        { 
          $this->strDataSource = 'Data Source='.$this->strRealPath; 
          $result = true; 
        } 
        else 
        { 
          echo "
    mdb::mdb() File not found $dsn
    "; $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 '
    mdb::open() ERROR with ADODB.Connection
    '.$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 ?>

    And an example:

    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 '
    '; $mdb->movenext(); } echo '


    '; # # 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 '
    '; $mdb->movenext(); } $mdb->close();

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

提交回复
热议问题