Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Unanswered: require help to include 2 databases in script

    Hello
    I have a website which connects to 2 databases on the same server (called mednigh and content). The version of mysql I was using was deprecated; I have managed to update the scripts using mysqli for 1 database, but am not sure about how to connect the 2nd database.
    Any help would be appreciated (this is quite new to me)…here are the principal pages:

    Dbcon.php
    <?php
    session_start();
    $site_path = "www.mysite.com/";

    if(!empty($_POST['lang'])) {

    $_SESSION['lang'] = $_POST['lang'];
    } elseif(isset($_SESSION['lang']))
    {
    $test = 1;
    }else{
    $_SESSION['lang'] = "Eng";
    }
    $con=mysqli_connect("localhost","user","password", " mednigh");
    if(!$con)
    {
    die('Couldnt connect to database:' .mysqli_error());
    }
    include('function.php');
    ?>

    Function.php
    <?php
    function getlg($lang,$name) {
    if($_SESSION['lang']){
    $lang = $_SESSION['lang'];
    }
    if($lang == '') {
    $lang = "Eng";
    }
    $con = new mysqli(("localhost","user","password"," mednigh");
    $query = "SELECT * FROM content WHERE name = '".$name."'";
    $result = mysqli_query($con,$query);
    if($result){
    while($row = mysqli_fetch_array($result)) {
    echo $row[$lang];
    }
    }
    }

    function getAllCountries()
    {
    $sql = "select * from country";
    $result = mysqli_query($con,$sql) or die (mysqli_error());
    return $result;
    }
    function getCountryTitle($id)
    {
    $sql = "select * from country where id = $id";
    $result = mysqli_query($con,$sql) or die (mysqli_error());
    $row = mysqli_fetch_array($result);
    return $row[country];
    }
    function getAllAreas()
    {
    $sql = "select * from area";
    $result = mysqli_query($con,$sql) or die (mysqli_error());
    return $result;
    }
    function getAreaTitle($id)
    {
    $sql = "select * from area where id = $id";
    $result = mysqli_query($con,$sql) or die (mysqli_error());
    $row = mysqli_fetch_array($result);
    return $row[area_name];
    }

    ?>
    Thanks

  2. #2
    Join Date
    Sep 2009
    Location
    San Sebastian, Spain
    Posts
    880
    There are two approaches you can take. The one is the way you are working now, however, I would change your coding to open the two connections at start of your code and then pass in the connection as a parameter into the functions so that it uses this to query the database. Something like this:

    Code:
    $mednigh = new mysqli("localhost","user","password"," mednigh");
    $content = new mysqli("localhost","user","password"," content");
    
    // If country table exists in mednigh then call it as follows:
    // $result = getAllCountries($mednigh);
    //
    function getAllCountries($con)
    {
    $sql = "select * from country";
    $result = mysqli_query($con,$sql) or die (mysqli_error());
    return $result;
    }
    The second option is to create FEDERATED tables between the two databases. A federated table allows you to create a virtual table inside one MySQL server database which accesses another table in another MySQL server and database.
    Ronan Cashell
    Certified Oracle DBA/Certified MySQL Expert (DBA & Cluster DBA)
    http://www.it-iss.com
    Follow me on Twitter

Posting Permissions

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