0

I just referenced this answer and what I preferred was very first solution, now the issue is he has given an information for mysql_() but am using mysqli_(), so using 4th parameter as true, I select the database when user logs in, the moment he logs in he gets redirected to respective page but it is showing that connection was actively refused. any Idea how I can use 2 database, 1 is my default engine database which I need to keep it on for running my framework and second database to run respective scripts according to the user logged in...

What am trying is this

<?php
    $database_connect = mysqli_connect('localhost', 'root', '', 'engine');
    if(!$database_connect) {
        die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    }

    if(isset($_SESSION['system_id'])) {
        $system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name'], true);
        if(!$system_database) {
            die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
        }
    }
?>

P.S I want a procedural way

Community
  • 1
  • 1
Random Guy
  • 2,878
  • 5
  • 20
  • 32

1 Answers1

0

You don't need any extra parameters, simply do it like this

<?php
    $database_connect = mysqli_connect('localhost', 'root', '', 'engine');
    if(!$database_connect) {
        die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    }

    if(isset($_SESSION['system_id'])) {
        $system_database = mysqli_connect('localhost', 'root', '', $_SESSION['system_name']);
        if(!$system_database) {
            die ('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
        }
    }

//Now whatever query you write, just do it like this

$access = mysqli_query($database_connection_var, "/*Query Goes Here*/"); //Will get database name from $database_connection_var

$access2 = mysqli_query($database_connection_var2, "/*Query Goes Here*/"); //Will get database name from $database_connection_var2
?>
Mr. Alien
  • 153,751
  • 34
  • 298
  • 278