PHP and Microsoft Access database - Connection and CRUD
PDO
If you want to interface with an MS Access database using PHP, PDO is available for you.
<?php
try {
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\accounts.mdb;Uid=Admin");
}
catch (PDOException $e) {
echo $e->getMessage();
}
When using PDO, due to the unified interface for DB operations, you have the opportunity to make your app more portable across various RDBMs systems. All you have to do is to provide the connection string to the PDO new instance and have the correct PDO driver installed.
As the result of this unified interface, your application can be easily ported from MS Access to MySQL, SQLite, Oracle, Informix, DB2, etc. which most certainly is the case if it ages enough.
Here's an insertion example:
<?php
try {
// Connect,
// Assuming that the DB file is available in `C:\animals.mdb`
$pdo = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\animals.mdb;Uid=Admin");
// INSERT data
$count = $pdo->exec("INSERT INTO animals(animal_type, animal_name) VALUES ('kiwi', 'troy')");
// echo the number of affected rows
echo $count;
// close the database connection
// See: http://php.net/manual/en/pdo.connections.php
$pdo = null;
}
catch (PDOException $e) {
echo $e->getMessage();
}
ODBC
In case you don't want to use PDO for some insane reasons, you can look into ODBC.
Here's an example:
<?php
if (! $conn = odbc_connect('northwind', '', '')) {
exit("Connection Failed: $conn");
}
if (! $rs = odbc_exec($conn, 'SELECT * FROM customers')) {
exit('Error in SQL');
}
while (odbc_fetch_row($rs)) {
echo 'Company name: ', odbc_result($rs, 'CompanyName'), PHP_EOL;
echo 'Contact name: ', odbc_result($rs, 'ContactName'), PHP_EOL;
}
odbc_close($conn);
PDO connect to .accdb on network
As a diagnostic check, try using the same mechanism you've been using to invoke your PDO script to invoke the following script:
<?php
$file = "\\\\server1\\abc\\123\\test.accdb";
if (is_file($file)) {
echo "File exists.\r\n";
}
else {
echo "File does not exist.\r\n";
}
That will test whether the account under which the script is running is allowed to access the network and "see" the file on the remote server. If it fails, then check the Windows rights for that account.
As a further test, you could try running that same script from the Windows command line using your regular user account (which presumably does have sufficient rights to access the network).
Simple Access to PHP
To migrate from one base to another database, the first thing is to have the same table structure.
Then, to read from a database of ACCESS can do something like ( http://es.php.net/manual/es/function.odbc-exec.php#71615 ):
$emp_id = $_GET['emp_id'];
$stmt = odbc_prepare($db_conn, "SELECT pwd FROM employees WHERE emp_id=?");
$res = odbc_execute($stmt, array($emp_id));
Then, with the results, you must perform the commands of inspection at the MySQL database ( http://es.php.net/manual/es/function.mysql-query.php). For example.
mysql_query('INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $values).'\')')
Related Topics
Bubble Sort Implementation in PHP
Getting a PHP Pdo Connection from a MySQL_Connect()
How to Reverse a Unicode String
What Is the Most Secure Method for Uploading a File
How to Get Exact Browser Name and Version
How to Validate Google Recaptcha on Form Submit
Belongstomany Relationship in Laravel Across Multiple Databases
Interface Implementation: Declaration Must Be Compatible
Get the Hierarchy of a Directory with PHP
Process Mathematical Equations in PHP
PHP Re-Order Array of Month Names