如何将pdo_odbc.ini扩展设置更改为libodbc.so。

huangapple go评论95阅读模式
英文:

How to change pdo_odbc.ini extension setting to libodbc.so

问题

在离线的Ubuntu 22.04服务器上安装了LAMP,用于测试odbc-mdbtools的设置,准备在在线服务器上进行读写MS Access以便与基于Microsoft的平板电脑发送/接收数据(在有人问为什么要这样做之前)。

首先安装了unixODBC,然后安装了odbc-mdbtools(后来才意识到它会自动添加)。编辑了php.ini以启用pdo-odbc扩展。在测试中没有收到任何响应,查看了phpinfo并发现pdo-odbc没有与php8.1一起安装。使用以下命令安装了它:

sudo apt-get install php-odbc

现在phpinfo输出中显示了odbc,但测试脚本仍然无法连接,而且显示尝试使用mysql引发了错误。因此,通过sudoedit编辑了pdo-odbc.ini文件:

#; php odbc模块的配置
; 优先级=20
extension=pdo_odbc.so

尝试将扩展更改为'libodbc.so',这是unixODBC的替代,但这导致测试脚本输出了'找不到驱动程序'的错误。扩展确实可用于'./usr/lib/x86_64-linux-gnu/libodbc.so'。

如何使php-pdo与它一起工作(甚至尝试卸载unixodbc和odbc-mbtools,然后重新安装只有odbc-mdbtools,希望它会将扩展添加到ini文件中)。

英文:

Installed LAMP on an offline Ubuntu 22.04 box to test odbc-mdbtools setup ready for an online server which needs to read and write to MS Access for sending/accepting with Microsoft-based tablets (before anyone says why do such a thing).

Put unixODBC on first, then odbc-mdbtools (before realising it would add it anyway).
Edited php.ini to enable the pdo-odbc extension.
Getting no response from tests, looked at phpinfo and spotted pdo-odbc wasn't installed with php8.1. Installed it with

suo apt-get install php-odbc

Now got odbc showing in phpinfo output but test script still not connecting but throwing as error showing it was trying to use mysql. So, did a sudoedit of pdo-odbc.ini:

#; configuration for php odbc module
; priority=20
extension=pdo_odbc.so

Tried altering the extension to 'libodbc.so' which is the unixODBC substitution but that produced an output from the test script saying there was 'no driver found'. The extension is certainly available './usr/lib/x86_64-linux-gnu/libodbc.so'

How can I get php-pdo to work with it (even tried purging unixodbc and odbc-mbtools and reinstalling just odbc-mdbtools in the hope it would pop the extension inot the ini file itself).

答案1

得分: 1

UPDATE 1

为了使它与PDO一起工作,看起来libodbccr存在问题,需要一个解决方法。

这是一个更新后的Dockerfile:

FROM ubuntu:22.04
RUN apt update
# tzdata是PHP的先决条件
# 这个以非交互方式安装tzdata
RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime; DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata; dpkg-reconfigure --frontend noninteractive tzdata
RUN apt install -y php php-odbc mdbtools mdbtools-dev

# 这将根据您的安装而需要更新。
# 要找到正确的目录,请运行"(cd / && find -name 'libodbccr.so*')"
RUN ln -s /usr/lib/aarch64-linux-gnu/libodbccr.so.2 /usr/lib/aarch64-linux-gnu/libodbccr.so

以及通过PDO访问数据库的简单脚本:

<?php
$db = "/working/MYDB.mdb";
$conn = new PDO("odbc:Driver=MDBTools;DBQ=//$db;UID=;PWD=;", '', '');
$data = $conn->prepare("SELECT * FROM my_table");
$data->execute();
$done = $data->fetch();
echo "foo = " . $done['AName'] . "\n";
echo "DONE\n";
?>

ORIGINAL

您是否安装了mdbtools-dev

我已经在Docker中使用以下Dockerfile启动它,而且它 "只需要工作",无需编辑任何php ini文件:

FROM ubuntu:22.04
RUN apt update
# tzdata是PHP的先决条件
# 这个以非交互方式安装tzdata
RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime; DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata; dpkg-reconfigure --frontend noninteractive tzdata
RUN apt install -y php php-odbc mdbtools mdbtools-dev

这是我组合的PHP代码,使其能够读取(这确实是一个粗略的工作,因为我不懂PHP):

<?php
$db = "/working/MYDB.mdb";
$conn = odbc_connect("Driver=MDBTools;DBQ=//$db;UID=;PWD=;", '', '');
$rs = odbc_exec($conn, "SELECT * FROM my_table");
odbc_fetch_row($rs);
$foo = odbc_result($rs, "AName");
echo "foo = $foo\n";
echo "DONE\n";
odbc_close($conn);
?>

这可以在命令行中使用原始PHP工作,可能与LAMP堆栈一起工作时会有其他特殊情况。如果有的话,请联系我,我会再次查看。

英文:

UPDATE 1

To make it work with PDO, it looks like there's an issue with libodbccr that needs a work-around.

Here's an updated Dockerfile:

FROM ubuntu:22.04
RUN apt update
# tzdata is a pre-requisite for PHP
# this install tzdata in a non-interactive fashion
RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime; DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata; dpkg-reconfigure --frontend noninteractive tzdata
RUN apt install -y php php-odbc mdbtools mdbtools-dev

# This will need to be updated per your installation.
# To find the right directory run &quot;(cd / &amp;&amp; find -name &#39;libodbccr.so*&#39;)&quot;
RUN ln -s /usr/lib/aarch64-linux-gnu/libodbccr.so.2 /usr/lib/aarch64-linux-gnu/libodbccr.so

And a trivial DB access script via PDO:

&lt;?php
$db = &quot;/working/MYDB.mdb&quot;;
$conn = new PDO(&quot;odbc:Driver=MDBTools;DBQ=//$db;UID=;PWD=;&quot;,&#39;&#39;,&#39;&#39;);
$data = $conn-&gt;prepare(&quot;SELECT * FROM my_table&quot;);
$data-&gt;execute();
$done = $data-&gt;fetch();
echo &quot;foo = &quot; . $done[&#39;AName&#39;] . &quot;\n&quot;;
echo &quot;DONE\n&quot;;
?&gt;

ORIGINAL

Have you installed mdbtools-dev?

I've booted it up in docker using the following Dockerfile and it "Just Works", without having to edit any php ini files:

FROM ubuntu:22.04
RUN apt update
# tzdata is a pre-requisite for PHP
# this install tzdata in a non-interactive fashion
RUN ln -fs /usr/share/zoneinfo/America/New_York /etc/localtime; DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata; dpkg-reconfigure --frontend noninteractive tzdata
RUN apt install -y php php-odbc mdbtools mdbtools-dev

And this is the PHP I hacked together to make it read (and it is a hack job since I don't speak PHP):

&lt;?php
$db = &quot;/working/MYDB.mdb&quot;;
$conn = odbc_connect(&quot;Driver=MDBTools;DBQ=//$db;UID=;PWD=;&quot;,&#39;&#39;,&#39;&#39;);
$rs = odbc_exec($conn,&quot;SELECT * FROM my_table&quot;);
odbc_fetch_row($rs);
$foo = odbc_result($rs, &quot;AName&quot;);
echo &quot;foo = $foo\n&quot;;
echo &quot;DONE\n&quot;;
odbc_close($conn);
?&gt;

This works with raw PHP from the command line, there may be other peculiarities working with a LAMP stack. If so ping me and I'll take another look.

huangapple
  • 本文由 发表于 2023年8月10日 17:28:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76874392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定