1
manumanu
using xoopsDB class with a separate database
  • 2004/10/8 16:13

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


hello,

I would like to use the xoopsDB object to connect to a separate database in my module.

In other words I need to fetch data from another database than XOOPS one, but I didn't manage to find how to set correctly the new database host,name...

Tried get_class_methods() but didn't found nothing to set DB connection data...

$xoopsDB seems to always use constants (XOOPS_DB_HOST, XOOPS_DB_USER, XOOPS_DB_PASS), so its impossible to connect to a different database?

thanks

2
ejuden01
Re: using xoopsDB class with a separate database
  • 2004/10/8 16:34

  • ejuden01

  • Not too shy to talk

  • Posts: 121

  • Since: 2004/4/5 1


Couldn't you just replace the constants with the db information that you are trying to connect to?

3
ejuden01
Re: using xoopsDB class with a separate database
  • 2004/10/8 16:43

  • ejuden01

  • Not too shy to talk

  • Posts: 121

  • Since: 2004/4/5 1


You might consider copying the mysqldatabase.php file from XOOPS_ROOT_PATH/class/database and put it in your module files with a different class name and variable names. That way, you could specify your own constants for the database connection and change them if your database changes. Anybody else have suggestions?

4
manumanu
Re: using xoopsDB class with a separate database
  • 2004/10/8 16:47

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


Thanks ejuden01 ,

I try your suggestion and reply soon

5
manumanu
Re: using xoopsDB class with a separate database
  • 2004/10/8 16:57

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


Tried this:
define(XOOPS_DB_HOST,'localhost');
define(XOOPS_DB_USER,'root');
define(XOOPS_DB_PASS,'pass');
define(XOOPS_DB_NAME,'second_database');
define(XOOPS_DB_PREFIX,''); 
//$databouse =  new secondDatabase();
$databouse =& XoopsDatabaseFactory::getDatabaseConnection();
$res $databouse->query('SELECT my_field FROM my_table');


and got this in database debug popup:

Quote:
SELECT my_field FROM my_table
Error number: 1146
Error message: Table 'intra_php.my_table' doesn't exist


(intra_php is my XOOPS_DB_PREFIX)

Since $xoopsDB seems to be created with $xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();, my customXOOPS_DB_PREFIX is ignored...

6
manumanu
Re: using xoopsDB class with a separate database
  • 2004/10/8 17:05

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


Another test:

Tried to wrap connect() method by extending XoopsMySQLDatabase class:

class secondDatabase extends XoopsMySQLDatabase {
    
    var 
$Host 'localhost';
    var 
$Database 'second_database';
    var 
$User 'root';
    var 
$Password 'pass';
    
    
    function 
connect($selectdb true)
    {
        if (
XOOPS_DB_PCONNECT == 1) {
            
$this->conn = @mysql_pconnect($this->Host$this->User$this->Password);
        } else {
            
$this->conn = @mysql_connect($this->Host$this->User$this->Password);
        }
    
        if (!
$this->conn) {
            
$this->logger->addQuery(''$this->error(), $this->errno());
            return 
false;
        }
        
        if(
$selectdb != false){
            if (!
mysql_select_db($this->Database)) {
                
$this->logger->addQuery(''$this->error(), $this->errno());
                return 
false;
            }
        }
        return 
true;
    }
    
}



and my standard query code:
$databouse = new secondDatabase();
$res $databouse->query('SELECT my_field FROM my_table');
...[
data fetch]...


... and nothing happens (even in php debug mode)

7
manumanu
Re: using xoopsDB class with a separate database
  • 2004/10/8 17:12

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


in mysqldatabase.php:

Quote:
perform a query
*
* This method is empty and does nothing! It should therefore only be
* used if nothing is exactly what you want done!


forget my previous post lol




With chance a XOOPS guru/dev may read this thread and have a solution?..

8
Mithrandir
Re: using xoopsDB class with a separate database

extend XoopsMySQLDatabaseSafe instead

The implementation of XoopsMySQLDatabase::query() does absolutely nothing because this is the method, which is overridden in XoopsMySQLDatabaseSafe and XoopsMySQLDatabaseProxy

9
manumanu
Re: using xoopsDB class with a separate database
  • 2004/10/8 17:51

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


thanks Mithrandir ,

Extended XoopsMySQLDatabaseSafe and got this :
Quote:
Warning [PHP]: mysql_query(): supplied argument is not a valid MySQL-Link resource in file X:\htdocs\xoops\class\database\mysqldatabase.php line 236


Line 236 is in XoopsMySQLDatabase::connect() method! (with XOOPS constants I dont want to use)

But I had redefined connect() in secondDatabase witch extends XoopsMySQLDatabaseSafe witch extends XoopsMySQLDatabase!

method redefinition works at only one level?

10
manumanu
Re: using xoopsDB class with a separate database>> ok
  • 2004/10/9 10:48

  • manumanu

  • Just popping in

  • Posts: 10

  • Since: 2002/5/15


Ok, its working

Here is the code if someone is interested:

class secondDatabase extends XoopsMySQLDatabaseSafe {
    
    var 
$Host 'host';
    var 
$Database 'database';
    var 
$User 'user';
    var 
$Password 'pass';
    
    function 
secondDatabase(){
    }
    
    function 
connect($selectdb true)
    {
        if (
XOOPS_DB_PCONNECT == 1) {
            
$this->conn = @mysql_pconnect($this->Host$this->User$this->Password);
        } else {
            
$this->conn = @mysql_connect($this->Host$this->User$this->Password);
        }
    
        if (!
$this->conn) {
            
$this->logger->addQuery(''$this->error(), $this->errno());
            return 
false;
        }
        
        if(
$selectdb != false){
            if (!
mysql_select_db($this->Database)) {
                
$this->logger->addQuery(''$this->error(), $this->errno());
                return 
false;
            }
        }
        return 
true;
    }
    
}

Note that this class uses XOOPS settings for persistent connexion or not...

and in the module:
$altDB= new secondDatabase();
$xoopsLogger=& XoopsLogger::instance();
$altDB->connect(true);
$altDB->setLogger(&$xoopsLogger);
$res $altDB->query('SELECT fields FROM table WHERE 1');
while (
$myrow $altDB->fetchArray($res)) {
    [
fetch your data]
}


thanks again

Login

Who's Online

463 user(s) are online (339 user(s) are browsing Support Forums)


Members: 0


Guests: 463


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits