PDA

View Full Version : working with 2 database help please


miz
02-01-2005, 07:46 AM
ok i have 2 dbs on same host
1 is vbulletin and the 2nd is just some info
i need about things

now mysql user have axx to both databases

so i thought ok vbulletin will use $DB_site and for the other info ill user
$predb

so when im doing $Db_site->query($query)
its will run on vbulletin db
and when im doing $predb->query($query) its will run on the other database

sounds simple right ?

so i edited config.php and added $predbname = '';
so i can use it for $predb

also i open init.php to add new db_class
and added this code :

$predb = new DB_Sql_vb;
$predb->appname = 'Pre';
$predb->appshortname = 'pre (' . VB_AREA . ')';
$predb->database = $predbname;

$predb->connect($servername, $dbusername, $dbpassword, $usepconnect);

now my problem is like that
every time server tries to connect to host he connect to db in $predb
so he wont find vbulletin info

now my qustion is if there any way that i can use this method
with out doing mysql_select_db every time i want to use $predb instead of $DB_site ???

help appricated. thanx
MiZ

Dean C
02-01-2005, 02:23 PM
To work with a seperate DB all you need to do is prefix the tablename in your queries with the database name.

e.g.


SELECT field
FROM dbname." . TABLE_PREFIX . "table as table

miz
02-01-2005, 10:22 PM
nahh i sloved it with better idea


define('VBDB', 'vbulletindb');
define('PREDB', 'myotherdb');

function switchdb()
{
global $DB_site;


switch ($DB_site->database)
{
case PREDB:
$DB_site->database = VBDB;
$DB_site->select_db(VBDB);

break;
case VBDB:
$DB_site->database = PREDB;
$DB_site->select_db(PREDB);

break;



}

}


then if i make a function
all i do is
switchdb();
on start and end of function...

To work with a seperate DB all you need to do is prefix the tablename in your queries with the database name.

e.g.


SELECT field
FROM dbname." . TABLE_PREFIX . "table as table


but you gave me an idea

i can add new define
like

define(MiZDB,$dbname);



and just to do



SELECT field
FROM " . MIZDB. "table as table


as my other table dosent user prefix

btw is my first way takes more qureys ?

Dean C
02-01-2005, 11:28 PM
Why make a function when you can just put $DB_site->select_db('dbname'); in your code before you run the query, and then switch it back afterwards, seems kinda redundant :)

miz
02-01-2005, 11:33 PM
Why make a function when you can just put$DB_site->select_db('dbname'); in your code before you run thequery, and then switch it back afterwards, seems kinda redundant :)

cuse ill need to to do a lot of switch db
but i made it on the easyest way that can be
in init.php i added :

define('PREDB','mydb.mytable');
// i need only 1 table form this db
// so why not make life eayer ?

after

define('TABLE_PREFIX', $tableprefix);


and on my script its looks like that :



$DB_site->query("SELECT * FROM ". PREDB ." ORDER BY id DESC LIMIT 5 ");



simeple as that.
so when i want to use my other db i use PRED in query