views:

107

answers:

1

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.

+1  A: 

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!?

Alex