在 Joomla 文章中使用 php ODBC 函数时出现问题

发布于 2024-09-19 04:41:46 字数 1537 浏览 1 评论 0原文

我是 Joomla 的新手,也是 php 的新手(希望我的年龄也这么新)。我已在本地 apache 网络服务器 上安装了 joomla。我正在尝试在 joomla 文章中使用一些 php 代码,以便从 Sybase ASE 12.5 数据库中获取一些数据。我安装了 sourcerer 并开始尝试使用系统 DSN 进行 ODBC 连接(我验证了它是工作):

{source}
<?php
echo 'This text is placed through <b>PHP</b>!';
echo '<p>';
echo '</p>';

$conn = odbc_connect('myDSN', 'sa', 'myPassword') or die('Could not connect !');
echo 'Connected successfully';

$sql = 'SELECT day, name FROM my_table where month = 1';

odbc_close($conn);

?>
{/source}

上面的代码没有做太多事情,但这就是我可以毫无问题地达到的程度。我刷新 joomla 页面,在文章的文本中看到:

...
This text is placed through PHP!

Connected successfully 
...

看起来没问题,连接显然已建立(我通过停止 sybase 服务并收到“无法连接”消息来验证这一点)。然后我在 $sql 赋值的下面又添加了一行。

$rs = odbc_exec($conn,$sql);

我刷新并...我没有看到脚本中的任何内容(甚至没有看到“此文本是通过 PHP 放置的!”)。 显然,如果我包含代码来回显 $rs 的内容,我什么也看不到。我也尝试过这个,但没有成功。

if (!$rs)
{exit("Error in SQL");}

添加 odbc_exec 命令后,整个脚本将停止工作。 在 php.ini 中我读到:

; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.

你知道出了什么问题吗?

  • 更新

使用代码 like 连接到 MySQL 数据库这就像一个魅力。

I am new to Joomla and new to php (wish I was so new in age too). I have installed joomla on a local apache webserver. I am trying to use some php code in a joomla article in order to fetch some data from a Sybase ASE 12.5 database. I installed sourcerer and started to try an ODBC connection using a system DSN (which I verified it is working):

{source}
<?php
echo 'This text is placed through <b>PHP</b>!';
echo '<p>';
echo '</p>';

$conn = odbc_connect('myDSN', 'sa', 'myPassword') or die('Could not connect !');
echo 'Connected successfully';

$sql = 'SELECT day, name FROM my_table where month = 1';

odbc_close($conn);

?>
{/source}

The above code doesn't do much, but this is how far I can get without problems. I refresh the joomla page and I see inside the article's text:

...
This text is placed through PHP!

Connected successfully 
...

Seems ok, the connection obviously established (I verified this by stopping the sybase service and getting the "Could not connect" message). Then I added one more line, just below the $sql assignment.

$rs = odbc_exec($conn,$sql);

I refresh and ...I see nothing coming from the script (not even the "This text is placed through PHP!").
Obviously, I see nothing if I include code to echo the contents of $rs. I also tried this but in vain.

if (!$rs)
{exit("Error in SQL");}

Once I add the odbc_exec command, the entire script ceases working.
In php.ini I read:

; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.

Do you have any idea what is going wrong?

  • UPDATE

Connecting to a MySQL database using code like this, works like a charm.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多彩岁月 2024-09-26 04:41:46

回答你的问题

这是获取数据的正确方法吗
从另一个数据库到一篇文章
或者我应该使用一些插件来做
那?

正确的方法是使用通过JFactory::getDBO()JDatabase::getInstance()获取的JDatabase对象,参见Joomla JDatabase 文档

$db = JDatabase::getInstance( $databasConfigArray );
$db->setQuery('your query');
$data = $db->loadObjectList();

这是一个很好的教程,展示了如何在 Joomla 中连接到多个数据库,甚至还有辅助类的源代码。

另请查看此线程Connecting to 3rd party databse in Joomla!?

To answer your question

Is this the correct way to get data
from another database in to an article
or should I use some plug-in to do
that?

The correct way is to use JDatabase object which you get by JFactory::getDBO() or JDatabase::getInstance(), see Joomla JDatabase documentation.

$db = JDatabase::getInstance( $databasConfigArray );
$db->setQuery('your query');
$data = $db->loadObjectList();

Here is a good tutorial showing how to connect to multiple databases in Joomla, there is even source code for helper class.

Also look at this thread Connecting to 3rd party databse in Joomla!?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文