Results 1 to 3 of 3

Thread: Connecting to 2 Mysql database in the same script??

  1. #1
    Frontpage User
    Join Date
    Apr 2010
    Posts
    1
    Post Thanks / Like

    Question Connecting to 2 Mysql database in the same script??

    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>";
        }
        
    }
    
    ?>

  2. #2
    respect my AW-THOR-IT-AYY Forbairt's Avatar
    Join Date
    Jun 2007
    Location
    My Office, Dublin
    Posts
    2,424
    Post Thanks / Like

    Default

    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)

  3. #3
    Wannabe Geek php.allstar's Avatar
    Join Date
    Apr 2009
    Location
    Monamolin, Gorey, Co. Wexford
    Posts
    405
    Post Thanks / Like

    Default

    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 Code:
    global $db2;

    class 
    className
    {

        function 
    functionName()
        {

            
    // rest of function code

        
    }

    }

    // or

    class className
    {

        function 
    functionName()
        {

            global 
    $db2;
            
    // rest of function code

        
    }



Similar Threads

  1. Updating a database using PHP/MySQL
    By iorrasnet in forum Coding Help
    Replies: 13
    Last Post: 11-03-2009, 12:08 PM
  2. Problem Inserting 2D array into mysql database.
    By daveharding in forum Coding Help
    Replies: 11
    Last Post: 22-11-2008, 03:57 PM
  3. Ajax and mysql and PHP, pulling info from a database
    By Ciarán Mc Cann in forum Coding Help
    Replies: 10
    Last Post: 09-08-2007, 12:26 PM
  4. Inserting PHP multidimensional arrays into a mySQL database
    By Ciarán Mc Cann in forum Coding Help
    Replies: 15
    Last Post: 03-08-2007, 06:15 PM
  5. Connecting to 2 Mysql database in the same script??
    By Ciarán Mc Cann in forum Coding Help
    Replies: 11
    Last Post: 24-07-2007, 10:21 AM

Visitors found this page by searching for:

SMS Donation script

l2j sms donate

php connect to 2 mysql databases l2j

php ile iki veritabanı

mysql connect to two databases in same script

SMS Donation Shop l2j

l2j php script how to set

connect to two databases in the same script

irish domains help connecting to database

sms donate l2jfrozen

php connect two database

connect db2 database using php

how to use two script same database

SMS Donate to L2J

sms donation scripts

php iki veritabanı

l2j_sms.html

update 2 databases same script

l2j sms

connect at 2 mysql databases in the same site

l2j connect 2 databases

irish domains database connection

sms donate system l2j

facebook sms.db account_name

connecting to 2 mysql databases in 1 php file

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •