Moderator |
|
Dit kan / zou moeten kunnen met ODBC. Zal eens kijken of ik daar nog ergens een voorbeeldje van heb, of er een kan maken. Ook zou je ondertussen wat kunnen Googlen .
EDIT: uitgeprobeerd op een eeuwenoude MS Access database die ik nog had liggen ergens. Dit voorbeeld houdt geen rekening met:
- security (dump-functie escaped geen output)
- speciale/uitgebreide karaktersets, dat moet je zelf maar uitvogelen
<?php
function dump($a) {
echo '<pre>'.print_r($a, true).'</pre>';
}
// assumption: no usernames/passwords set for accessing
$source = getcwd().DIRECTORY_SEPARATOR.'db.mdb';
if (($con = odbc_connect('Driver={Microsoft Access Driver (*.mdb)};Dbq='.$source, '', '', SQL_CUR_USE_ODBC)) === false) {
die('connection failed');
}
// odbc_prepare will only halt on syntax error, it isn't until execution a query fails,
// for example because you refer to a a nonexistent table...
// odbc_exec both prepares and executes a query (so it's odbc_prepare() and odbc_execute() combined)
// for a list of tables use odbc_tables
$tables = array();
$res = odbc_tables($con);
while ($row = odbc_fetch_array($res)) {
if ($row['TABLE_TYPE'] == 'TABLE') {
$tables[] = $row['TABLE_NAME'];
}
}
odbc_free_result($res);
dump($tables);
// for a table definition
// @see http://www.php.net/manual/en/function.odbc-columns.php comments
// cannot seem to get odbc_columns to work, or I don't understand how to use
$table = array();
$res = odbc_exec($con, 'SELECT * FROM someTable WHERE 1=2');
$n = odbc_num_fields($res); // field numbering starts at 1
for ($i=1; $i <= $n; $i++) {
$field_name = odbc_field_name($res, $i);
$table[$field_name] = array(
'type' => odbc_field_type($res, $i),
'length' => odbc_field_len($res, $i),
);
}
odbc_free_result($res);
dump($table);
// fetch some data
$res = odbc_exec($con, 'SELECT * FROM someTable') or die(odbc_error($con));
while ($row = odbc_fetch_array($res)) {
dump($row);
}
odbc_free_result($res);
odbc_close($con);
?>
<?php function dump($a) { } // assumption: no usernames/passwords set for accessing $source = getcwd().DIRECTORY_SEPARATOR .'db.mdb'; if (($con = odbc_connect('Driver={Microsoft Access Driver (*.mdb)};Dbq='.$source, '', '', SQL_CUR_USE_ODBC)) === false) { die('connection failed'); } // odbc_prepare will only halt on syntax error, it isn't until execution a query fails, // for example because you refer to a a nonexistent table... // odbc_exec both prepares and executes a query (so it's odbc_prepare() and odbc_execute() combined) // for a list of tables use odbc_tables $res = odbc_tables($con); while ($row = odbc_fetch_array($res)) { if ($row['TABLE_TYPE'] == 'TABLE') { $tables[] = $row['TABLE_NAME']; } } odbc_free_result($res); dump($tables); // for a table definition // @see http://www.php.net/manual/en/function.odbc-columns.php comments // cannot seem to get odbc_columns to work, or I don't understand how to use $res = odbc_exec($con, 'SELECT * FROM someTable WHERE 1=2'); $n = odbc_num_fields($res); // field numbering starts at 1 for ($i=1; $i <= $n; $i++) { $field_name = odbc_field_name($res, $i); $table[$field_name] = array( 'type' => odbc_field_type($res, $i), 'length' => odbc_field_len($res, $i), ); } odbc_free_result($res); dump($table); // fetch some data $res = odbc_exec ($con, 'SELECT * FROM someTable') or die(odbc_error ($con)); while ($row = odbc_fetch_array($res)) { dump($row); } odbc_free_result($res); odbc_close($con); ?>
|