1

I'm working on something that was built on a PC set-up using php and an ms access database. When I port the app to my MAMP environment, I get

Fatal error: Call to undefined function odbc_connect() in /path/to/index.php on line 37

line 37 looks like this:

return odbc_connect("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=myfile.mdb",
"ADODB.Connection", "", "SQL_CUR_USE_ODBC");

It seems like odbc isn't compiled into the MAMP version of PHP (5). I also tried using PDO, and got similar errors.

Anyone know how to fix this?

Adam Bellaire
  • 108,003
  • 19
  • 148
  • 163
sprugman
  • 19,351
  • 35
  • 110
  • 163
  • Despite the fact that MS has used "Microsoft Access Driver" as the name of its ODBC driver for Jet data, you're not using Access at all, only Jet. I wish they'd not named it that way because it would save a world of confusion. – David-W-Fenton Oct 24 '08 at 03:35

1 Answers1

3

You will need to add an ODBC driver like Actual ODBC to your machine, that is if your version of PHP had any ODBC functionality complied in, which it should have, but if not you'll need to install a different version with the appropriate support. I've had good luck with using MacPorts to install PHP. But note that there are still some functions missing that you may expect you'll have to write wrappers for these functions like so:

  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);
        }
    }
  }
Rob Booth
  • 1,792
  • 1
  • 11
  • 22