Created
March 5, 2022 10:06
-
-
Save catwhocode/eae0c649fdc66229bfc6a63f48715f31 to your computer and use it in GitHub Desktop.
PHP: Accessing MS Access Database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head><title>phpAccessBrowser</title> | |
<style> | |
hr {border:1px solid #ccc;} | |
table th,td {border:solid 1px #ccc;padding:5px;} | |
table tr:hover {background-color:#00ffff;} | |
.tablename {font-size:larger;font-weight:bold;margin-bottom:10px;background-color:#ccc;padding:5px;margin-top:-5px;} | |
</style> | |
</head> | |
<body> | |
<?php | |
$username = 'Admin'; | |
$password = ''; | |
$db_name = '/xampp/htdocs/phpAccessBrowser/Northwind.mdb'; | |
$db = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$db_name;", $username, $password); | |
$result = odbc_tables($db); | |
echo '<h3>' . $db_name . '</h3>'; | |
echo '<div id="top">..</div><table><tr><th>NO</th><th>TABLE</th></tr>'; | |
$table_counter = 1; | |
while (odbc_fetch_row($result)){ | |
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){ | |
$table_name = odbc_result($result,"TABLE_NAME"); | |
echo '<tr><td>' . $table_counter . '</td><td><a href="#' . $table_name . '">' . $table_name . '</a></td></tr>'; | |
$table_counter++; | |
} | |
} | |
echo '</table><hr>'; | |
$result = odbc_tables($db); | |
while (odbc_fetch_row($result)){ | |
if(odbc_result($result,"TABLE_TYPE")=="TABLE"){ | |
$table_name = odbc_result($result,"TABLE_NAME"); | |
echo '<div class="tablename" id="' . $table_name . '">' . $table_name . ' | <a href="#top">top</a></div>'; | |
$cols = odbc_exec($db, "SELECT * FROM $table_name WHERE 1=2"); | |
$ncols = odbc_num_fields($cols); | |
echo '<table><tr><th>NO</th><th>FIELD</th><th>TYPE</th><th>WIDTH</th><th>PRECISION</th></tr>'; | |
$field_counter = 1; | |
for ($n=1; $n<=$ncols; $n++) { | |
$field_name = odbc_field_name($cols, $n); | |
$field_type = odbc_field_type($cols, $n); | |
$field_length = odbc_field_len($cols, $n); | |
$field_precision = odbc_field_precision($cols, $n); | |
echo '<tr><td>' . $field_counter . '</td><td>' . $field_name . '</td><td>' . $field_type . '</td><td>' . $field_length . '</td><td>' . $field_precision . '</td></tr>'; | |
$field_counter++; | |
} | |
echo '</table>'; | |
echo '<hr>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment