How do I add an ODBC driver to a MAMP environment?

耗尽温柔 提交于 2019-12-02 09:57:09

问题


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?


回答1:


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);
        }
    }
  }


来源:https://stackoverflow.com/questions/223165/how-do-i-add-an-odbc-driver-to-a-mamp-environment

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!