0
votes

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 blank page is usually indicative of a fatal error, like a parse error. Turn on error reporting with ini_set('display_errors', 1); at the top of the script (alternatively, I think Joomla has a backend interface to turn on error reporting in the Global Configuration>Server Tab). If this is an error from the database you can use odbc_error(). Try odbc_exec($conn,$sql) or die(odbc_error()); to see if any useful info is produced.webbiedave
Iam not getting a blank page. I get the article's page as if there were no script included whatsoever!George Dontas
@db047, is this code part of plug-in/component or a separate page included as a wrapper? Anyways, enabled error_reporting and see what is causing the problem...Alex
This is the code that this plug in allows me to insert in the article's text. See this nonumber.nl/images/stories/extensions/sourcerer/…. Anyway, I tried the above but to no avail. I always see the rest of article's text without getting any errors, as if there's no php script at all.George Dontas
In general the question is: 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?George Dontas

1 Answers

1
votes

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