Forums

Problem accessing database

ricardo_arango 20 Dec, 2007
I need to display some info on my form, read from a table in my database, not created with chronoforms. The SQL qurey is ok as I have tested it separately. My code fails in the final line:


<?php
  global $database;
  
  
  $sql = "SELECT * from `vuelos` order by llegada_hora, fecha DESC;";
        
  $database->setQuery( $sql); /* Fails here */
         
?>
<br><br>Post edited by: ricardo_arango, at: 2007/12/20 10:09
Max_admin 20 Dec, 2007
Hi Ricardo,

Please remove the ; from the query and include another line to actually fetch the results into a usable array :


$database->setQuery($sql);
$vuelos = $database->loadObjectList();


cheers

Max
Max
ChronoForms developer...
Did you try ChronoMyAdmin for managing your Joomla database tables ?
ricardo_arango 20 Dec, 2007
Nope that didn't do it.

Could it be something related to the site configuration, or do I have to include any file in the form's code?

How can I check if the variable $database actually holds something? I tried echo $database but it doesn't output anything.
GreyHead 20 Dec, 2007
Hi Ricardo,

$database is an instance of the Joomla database class, if you got it to print I suspect that you'd just have a screen full of text.

Clearly $database is working because ChronoForms uses it to store the form info (among other things).

When you say 'Fails here' what do you mean? Do you get an error message? or a blank screen?

Your code should look like this:
<?php
global $database;
$sql = "SELECT * FROM `vuelos` ORDER BY llegada_hora, fecha DESC";
$database->setQuery($sql);
// these lines should give you an error message on failure. 
if (!$database->query()) {
  echo $database->stderr();
}
$vuelos = $database->loadObjectList();
// Caution, the next line will print the whole object list
// print_r($vuelos);         
?>
Please see if this gives you a meaningful error message on failure.

Bob
ricardo_arango 20 Dec, 2007
I tried your code, and after checking one line at the time, I found the code failing here:


$database->setQuery($sql);


And this is the message I get in Firefox:

Fatal error: Call to a member function on a non-object in /home/xbyxlukx/public_html/components/com_chronocontact/chronocontact.html.php(55) : eval()'d code on line 6




In case it's needed I'm using Joomla 1.5 RC3. Legacy support is not enabled.<br><br>Post edited by: ricardo_arango, at: 2007/12/20 17:21
GreyHead 20 Dec, 2007
Hi Ricardo,

Hmmm . . . scratches head . . . please will you take a backup copy of the form from the ChronoForms Form Manager and post it here or email it to me at the address in my sig.

Which versions of Joomla & ChronoForms are you using?

Bob
ricardo_arango 20 Dec, 2007
ChronoForms Version: 2.5 J1.5 RC1
Joomla: 1.5 RC3

The backup copy of the form, using your code. I checked it without the PHP code and it was ok:
[file name=vuelos.cfbak size=4341]http://www.chronoengine.com/components/com_fireboard/uploaded/files/vuelos.cfbak[/file]<br><br>Post edited by: ricardo_arango, at: 2007/12/20 17:37
GreyHead 20 Dec, 2007
Hi Ricardo,

Sorry, please attach it as a file - too much gets corrupted when it comes in as code. Thanks

Bob
ricardo_arango 20 Dec, 2007
I also have another form for contact which is working fine. I don't use PHP in it, but it might help. [file name=Contacto.cfbak size=10045]http://www.chronoengine.com/components/com_fireboard/uploaded/files/Contacto.cfbak[/file]<br><br>Post edited by: ricardo_arango, at: 2007/12/20 17:58
ricardo_arango 20 Dec, 2007
OK, fixed it

I used this

$database =& JFactory::getDBO();

to get the database object.

And it works!

Thanks for your help Bob.
This topic is locked and no more replies can be posted.