Come faccio ad aggiungere un driver ODBC a un ambiente MAMP?
Domanda
Sto lavorando a qualcosa che è stato creato su un PC configurato usando php e un database di accesso ms. Quando porto l'app nel mio ambiente MAMP, ottengo
Fatal error: Call to undefined function odbc_connect() in /path/to/index.php on line 37
La linea 37 è simile alla seguente:
return odbc_connect("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=myfile.mdb",
"ADODB.Connection", "", "SQL_CUR_USE_ODBC");
Sembra che odbc non sia compilato nella versione MAMP di PHP (5). Ho anche provato a utilizzare PDO e ho riscontrato errori simili.
Qualcuno sa come risolvere questo problema?
Soluzione
Dovrai aggiungere un driver ODBC come ODBC effettivo alla tua macchina, cioè se la tua versione di PHP aveva una funzionalità ODBC rispettata, che avrebbe dovuto, ma in caso contrario dovrai installare una versione diversa con il supporto appropriato. Ho avuto fortuna con l'utilizzo di MacPorts per installare PHP. Ma nota che mancano ancora alcune funzioni che potresti aspettarti che dovrai scrivere wrapper per queste funzioni in questo modo:
if(!function_exists("odbc_fetch_array"))
{
function odbc_fetch_array($aResult,$anAssoc=false)
{
# Out of rows? Pass back false!
if(!odbc_fetch_row($aResult)) return false;
$theRow = array();
# Build up array
$theNumFields = odbc_num_fields($aResult);
$theLimit = $theNumFields+1;
for($i=1; $i<$theLimit; $i++)
{
# WARNING: Starts our index at 0, unlike standard ODBC which starts at 1
$theRow[odbc_field_name($aResult, $i)] = odbc_result($aResult, $i);
if(!$anAssoc) $theRow[$i-1] = $theRow[odbc_field_name($aResult, $i)];
}
return $theRow;
}
}
if(!function_exists("odbc_fetch_assoc"))
{
function odbc_fetch_assoc($aResult)
{
if (DIRECTORY_SEPARATOR == '/') // call local function on MACs
{
return odbc_fetch_array($aResult,true);
}
else // call built in function on Windows
{
return odbc_fetch_array($aResult);
}
}
}