Connecting to 2 Mysql database in the same script??

Status
Not open for further replies.

hypogeum

New Member
Hi .
I got ucp + sms donate system created for l2j server and i want to use it for diferent server
And i dont know how to config it, how to get connect with two databases : au_server_ls and au_server_gs

config.php
Code:
<?php

$host = 'localhost';
$user = 'root';
$pass = 'root';
$db = 'au_server_gs';

$host2 = 'localhost';
$user2 = 'root';
$pass2 = 'root';
$db2 = 'au_server_ls';

?>

This is base.php where going all configs and dont know how to add second db
Code:
<?php
define('STARTED',true);
header("Content-type: text/html; charset=utf-8"); 
ob_start();
session_start();

include_once('config/mysql_config.php');
include_once('classes/mysql.class.php');
include_once('classes/validation.class.php');
include_once('classes/custom.class.php');
include_once('classes/template.class.php');

//mysql nustatymai
$db = new mysql_db($host, $user, $pass, $db);
$valid = new validation;
$custom = new custom;

$sql = "SELECT * FROM don_config";
$config = $db->get_array($sql);
foreach($config as $key => $set)
{

    $admin_ip = $set['admin_ip'];
    $admin_pass = $set['admin_pass'];
    $language = $set['language'];
    $template = $set['template'];

}

$tpl = new Rtemplate('points.tpl');
$tpl = new Rtemplate('shop.tpl');


include_once('language/'.$language.'/main.lng');

?>

custom.class.php
Code:
<?php

if(!defined('STARTED')) exit('No direct script access allowed');

class custom 
{

    public function login()
    {
    global $lang;
    global $db;
    
    $login = 
    "
        <link rel='stylesheet' href='engine/static_style/static.css' type='text/css'/>
        <form action='index.php' method='post'>
        <table class='login'>
        <input type='hidden' name='submit'>

        <tr><td>{$lang['slapyvardis']}:<br /><input type='text' name='login' class='admin'></td></tr>
        <tr><td>{$lang['slaptazodis']}: <br /><input type='password' name='password' class='admin'></td></tr>
        <tr><td align='center'><input type='submit' name='jungtis' class='admin' value='{$lang['prisijungti']}'></td></tr>
        </table>
        </form>
    ";
    
    if(isset($_SESSION['error_1'])){echo $_SESSION['error_1']; unset($_SESSION['error_1']);}
    if(isset($_SESSION['error_2'])){echo $_SESSION['error_2']; unset($_SESSION['error_2']);}
    if(isset($_SESSION['error_3'])){echo $_SESSION['error_3']; unset($_SESSION['error_3']);}
    if(isset($_SESSION['not_logged_in'])){echo $_SESSION['not_logged_in']; unset($_SESSION['not_logged_in']);}
    
    if(isset($_POST['jungtis']))
    {
    
        $error = array();
        
        $login = $_POST['login'];
        $password = $_POST['password'];
        
        $sql = "SELECT * FROM account_data WHERE name = '{$db->escape_str($login)}' AND password = '".base64_encode(pack("H*", sha1(utf8_encode($db->escape_str($password)))))."'";
        $check = $db->num_rows($sql);
        
        if(empty($login) || empty($password))
        {
            $_SESSION['error_1'] = "<li class='failed_login'>Užpildykite visus laukelius</li>";
            $error = 1;
        }
        
        if(!empty($login) && !empty($password) && $check == 0)
        {
        
            $_SESSION['error_3'] = "<li class='failed_login'>Neteisingas slaptažodis arba slapyvardis</li>";
            $error = 1;
        
        }
    
        if(count($error) == 0)
        {
        
            $sql2 = "SELECT * FROM don_game_r_info WHERE account_name = '{$db->escape_str($login)}'";
            $check2 = $db->num_rows($sql2);
            
            if($check2 == 0)
            {
                $sql3 = "INSERT INTO don_game_r_info SET account_name = '$login', points = '0'";
                $db->query($sql3);
                $_SESSION['user_real'] = $login;
                header('Location: user.php');
            }
            else
            {
            
                $_SESSION['user_real'] = $login;
                header('Location: user.php');
            
            }
        
        }
        else
        {
        
            header('Location: index.php');
            
        }
    
    }
    else
    {
        return $login;
    }
    }
    
    public function account_name()
    {
    
        global $db;
        global $lang;
        
        $sql = "SELECT account_name FROM don_game_r_info WHERE account_name = '{$db->escape_str($_SESSION['user_real'])}'";
        $name = $db->get_array($sql);
        foreach($name as $key => $set)
        {
            return $set['account_name'];
        }
    
    }
    
    public function account_points()
    {
        
        global $db;
        global $lang;
        
        $sql = "SELECT points FROM don_game_r_info WHERE account_name = '{$db->escape_str($_SESSION['user_real'])}'";
        $points = $db->get_array($sql);
        foreach($points as $key => $set)
        {
            return $set['points'];
        }
    
    }
    
    public function choose_account()
    {
    
        global $db;
        global $lang;
        
        $sql = "SELECT char_name, obj_Id FROM players WHERE account_name = '".$_SESSION['user_real']."'";
        $choose_account = $db->get_array($sql);
        foreach($choose_account as $key => $row)
        {
        
            $info[]['link'] = "<a href='shop.php?char=".$row['obj_Id']."'>".$row['char_name']."</a>";

        }
        
        return $info;
    
    }
    
    public function shop()
    {
    
        global $db;
        global $lang;
        
        $sql = "SELECT * FROM don_product";
        $prod = $db->get_array($sql);
        $i = 0;
        foreach($prod as $key => $row)
        {
            $i++;
            
            $pro[$i]['id'] = $row['id'];
            $pro[$i]['item_name'] = $row['item_name'];
            $pro[$i]['item_id'] = $row['item_id'];
            $pro[$i]['item_price'] = $row['item_price'];
            $pro[$i]['item_sum'] = $row['item_sum'];
            $pro[$i]['buy_link'] = "<a href='buy.php?char=".$_GET['char']."&id=".$row['id']."'>{$lang['pirkti']}</a>";
            
        
        }
        
        return $pro;
    
    }
    
    public function keywords()
    {
    
        global $db;
        global $lang;
        
        $sql = "SELECT * FROM don_sms_config";
        $keywords = $db->get_array($sql);
        $i = 0;
        foreach($keywords as $key => $set)
        {
            $i++;
            
            $price = $set['price'];
            $all_price = $price / 100;
            
            $keyword[$i]['keyword'] = $set['keyword'];
            $keyword[$i]['price'] = $all_price;
            $keyword[$i]['number'] = $set['number'];
            $keyword[$i]['sms_points'] = $set['sms_points'];
            
            
        }
        
        return $keyword;
    
    }
    
    public function back_button()
    {
        global $lang;
    
        return "<a href='javascript: history.go(-1)'>{$lang['atgal']}</a>";
    
    }
    
    public function title_main()
    {
    
        global $lang;
        $title = "{$lang['pavadinimas']}";
        return $title;
    
    }
    
    public function title_admin()
    {
    
        global $lang;
        $title = "{$lang['admin_ pavadinimas']}";
        return $title;
    
    }
    
    public function add_points()
    {
        
        global $lang;
        return "<a href='add.points.php'>{$lang['papildyti_saskaita']}</a>";
    
    }
    
    public function logout()
    {
        
        global $lang;
        
        if(isset($_GET['logout']))
        {
        session_destroy();
        header('Location: index.php');
        }
    
        return "<a href='user.php?logout'>Atsijungti</a>";
    }
    
}

?>
 

Forbairt

Teaching / Designing / Developing
Erm not really liking some of your code there but anyways ...

$db = new mysql_db($host, $user, $pass, $db);

So to create another database connection you'd do the exact same

$db2 = new mysql_db($host2, $user2, $pass2, $db2);

And then depending on which database you need to query use $db or $db2
$db->query(your sql stuff)
 

php.allstar

New Member
To add to forbairt's solution and depending on your needs and how often you need to call the $db2 object, you may need to declare $db2 as a global or put in in the global scope.

PHP:
global $db2;

class className
{

    function functionName()
    {

        // rest of function code

    }

}

// or

class className
{

    function functionName()
    {

        global $db2;
        // rest of function code

    }

}
 
Status
Not open for further replies.
Top