[英]Problem using php ODBC functions from within a Joomla article
I am new to Joomla and new to php (wish I was so new in age too). 我是Joomla的新手,也是php的新手(真希望我也这么年轻)。 I have installed joomla on a local apache webserver . 我已经在本地apache网络服务器上安装了joomla。 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. 我试图在joomla文章中使用一些php代码,以便从Sybase ASE 12.5数据库中获取一些数据。 I installed sourcerer and started to try an ODBC connection using a system DSN (which I verified it is working): 我安装了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}
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: 我刷新了joomla页面,然后在文章的文本中看到了:
...
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). 似乎还可以,显然可以建立连接(我通过停止sybase服务并获得“ 无法连接 ”消息来验证这一点)。 Then I added one more line, just below the $sql assignment. 然后,我在$ sql分配下面添加了另一行。
$rs = odbc_exec($conn,$sql);
I refresh and ...I see nothing coming from the script (not even the "This text is placed through PHP!" ). 我刷新并...我没有看到任何来自脚本的消息(甚至没有“该文本通过PHP放置!” )。 Obviously, I see nothing if I include code to echo the contents of $rs. 显然,如果包含回显$ 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. 添加odbc_exec
命令后,整个脚本将停止工作。 In php.ini I read: 在php.ini中,我读到:
; 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? 你有什么想法吗?
Connecting to a MySQL database using code like this , works like a charm. 连接到使用代码MySQL数据库是这样 ,就像一个魅力。
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 . 正确的方法是使用通过JFactory::getDBO()
或JDatabase::getInstance()
获得的JDatabase
对象,请参见Joomla JDatabase文档 。
$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. 这是一个很好的教程,展示了如何连接到Joomla中的多个数据库,甚至还有帮助程序类的源代码。
Also look at this thread Connecting to 3rd party databse in Joomla!? 还要看看这个线程连接到Joomla中的第三方数据库!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.