Reputation: 167
How to load .mdb file into mysql database using php code.
I have a .mdb file and location is /example/employee.mdb. I need to read record from attendance table and insert it into mysql's emp_attendance table.
What will be PHP logic for that?
Upvotes: 3
Views: 10133
Reputation: 167
Finally Below logic worked for me.
$fileNm = "C:\Users\abc\Desktop\attendanceData.mdb";
$conn = new COM("ADODB.Connection") or die("ADODB Connection Faild!");
$conn->Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=$fileNm");
if($conn){
$data = $conn->Execute("SELECT * FROM em_attendance");
}
Upvotes: 2
Reputation: 107567
Like most relational databases, PHP can connect to MS Access and MySQL with PDO. Consider doing so with a dual connection with MySQL. No external, third-party software is required. Below example uses a mock table and fields. Adjust as needed:
Requirements include (no MSAccess.exe installation required):
PHP Dual PDO Connection
$accdatabase="C:\Path\To\database.accdb";
$host="localhost";
$mydatabase="****";
$username="****";
$password="****";
try {
# OPEN BOTH DATABASE CONNECTIONS
$accConn = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBq=$accdatabase;Uid=Admin;Pwd=;");
$myConn = new PDO("mysql:host=$host;dbname=$mydatabase",$username,$password);
$myConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM emp_attendance";
$accstmt = $accConn->query($sql);
$accstmt->setFetchMode(PDO::FETCH_ASSOC);
// FETCH ROWS FROM MS ACCESS
while($row = $accstmt->fetch()) {
// APPEND TO MYSQL
$mystmt = $myConn->prepare("INSERT INTO emp_attendance (empid, `date`, status, notes) VALUES (?, ?, ?, ?)");
# BIND PARAMETERS
$mystmt->bindParam(1, $row['empid'], PDO::PARAM_STR, 50);
$mystmt->bindParam(2, $row['date'], PDO::PARAM_STR, 50);
$mystmt->bindParam(3, $row['status'], PDO::PARAM_STR, 50);
$mystmt->bindParam(4, $row['notes'], PDO::PARAM_STR, 50);
# EXECUTE QUERY
$mystmt->execute();
}
}
catch(PDOException $e) {
echo $e->getMessage()."\n";
exit;
}
// CLOSE CONNECTIONS
$accConn = null;
$myConn = null;
Upvotes: 3
Reputation: 79
MDB is a database file used by Microsoft Access. MDB is Access’s own format which is based on the Access Jet Database Engine. You can open it by Microsoft Access as it is it's own format, also can open it by Microsoft Excel, If you don't have Microsoft Office you don't need to worry about. MDB files can also be opened by OpenOffice.org, SAS Institute SAS, Wolfram Mathematica, Softpedia MDB Converter¸ and Microsoft Visual Studio 2010. MDB file files can be converted to .TXT or .CSV formats using a MDB Converter. Even if you don't have above all you can download the a free popular tool MDB Viewer Plus (http://download.cnet.com/MDB-Viewer-Plus/3001-10254_4-75285626.html?onid=10254) Read datas and insert into database however you want. Better convert the file mdb to csv and insert into database. It's easier using by PHP. Hope you have your answer
Steps to Convert Microsoft Access Files to CSV Format
Hope your problem will be solved.Thanks
Upvotes: 0
Reputation: 77
read the data from mdb to csv
read from csv and insert into mysql
Or
Try this
http://board.phpbuilder.com/showthread.php?10365863-Convert-mdb-to-mysql
Upvotes: 0