I have two databases and I want to select a database on userid password condition, but right now it is not working with this condition. How can I achieve it? For now, only the first database gets selected, not the second one.
<?php
if($_SESSION['s_activId'] == 'om' && isset($_SESSION['s_userType']) == 'om')
{
$dbName = "kal";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("colud not connect to database". mysql_error());
}
else if($_SESSION['s_activId'] == 'om' && isset($_SESSION['s_userType']) == 'om1')
{
$dbName = "kal1";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("colud not connect to database". mysql_error());
}
?>
If you are using $_SESSION
in a script you must start the session or it is not visible.
Also the If tests needed a little correction, isset()
returns a boolean and not the data held in the array occurance.
<?php
// add this to gain access to the session
session_start();
if($_SESSION['s_activId'] == 'om' &&
isset($_SESSION['s_userType']) &&
$_SESSION['s_userType'] == 'om')
{
$dbName = "kal";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("could not connect to database". mysql_error());
}
else if($_SESSION['s_activId'] == 'om' &&
isset($_SESSION['s_userType']) &&
$_SESSION['s_userType'] == 'om1')
{
$dbName = "kal1";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("could not connect to database". mysql_error());
}
?>
Try this:
<?php
session_start(); //starts the session, then you can access its members
if($_SESSION['s_activId'] == 'om' && $_SESSION['s_userType'] == 'om')
{
$dbName = "kal";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("could not connect to database". mysql_error());
}
else if($_SESSION['s_activId'] == 'om' && $_SESSION['s_userType'] == 'om1')
{
$dbName = "kal1";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("could not connect to database". mysql_error());
}
?>
You can't compare isset
to a string, since it returns true/false. Also you must use session_start()
in a file where you will use sessions.
Fixing your code it would be:
<?php
session_start();
if(isset($_SESSION['s_activId']) && isset($_SESSION['s_userType']
&& $_SESSION['s_activId'] == 'om' && $_SESSION['s_userType']=='om')
{
$dbName = "kal";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("colud not connect to database". mysql_error());
}
else if(isset($_SESSION['s_activId']) && isset($_SESSION['s_userType']
&& $_SESSION['s_activId'] == 'om1' && $_SESSION['s_userType']=='om1')
{
$dbName = "kal1";
$link = mysql_connect("localhost","root","");
mysql_select_db($dbName) or die("colud not connect to database". mysql_error());
}
?>
But a better approach for this code would be to use mysqli since mysql is deprecated.
<?php
session_start();
if(isset($_SESSION['s_activId']) && isset($_SESSION['s_userType'] )
{
if ($_SESSION['s_activId'] == 'om' && $_SESSION['s_userType']=='om')
{
$dbName = "kal";
$link = mysqli_connect("localhost","root","", $dbName);
}
else if($_SESSION['s_activId'] == 'om1' && $_SESSION['s_userType']=='om1')
{
$dbName = "kal1";
$link = mysqli_connect("localhost","root","",$dbName);
}
}
?>
That should work.
If you are using other mysql functions the second code won't work unless you change all of them to mysqli.
For example mysql_fetch_array($result)
would become mysqli_fetch_array($link, $result)
Don't Use mysql_ functions. They are deprecated. Use mysqli_ or prepared statements.
[Note: The MySQLi extension is designed to work with MySQL version 4.1.13 or newer.]
session_start();
if($_SESSION['s_activId'] == 'om' && isset($_SESSION['s_userType']) && $_SESSION['s_userType'] == 'om')
{
$dbName = "kal";
$link = mysqli_connect("localhost","root","",$dbName) or die("could not connect to database". mysqli_connect_error();
}
else if($_SESSION['s_activId'] == 'om' && isset($_SESSION['s_userType']) && $_SESSION['s_userType'] == 'om1')
{
$dbName = "kal1";
$link = mysqli_connect("localhost","root","",$dbName) or die("could not connect to database". mysqli_connect_error();
}
?>
try this code, probably it will help. The code is straight forward in order to debug your issue use
echo print_r($_SESSION, true);
To see the values you gonna use.
<?php
session_start();
$sActivId = isset($_SESSION['s_activId']) ? $_SESSION['s_activId'] : "";
$sUserType = isset($_SESSION['s_userType']) ? $_SESSION['s_userType'] : "";
$dbName = "";
if($sActivId != 'om'){
throw new Exception("The user is not valid or active");
}
switch($sUserType){
case 'om':
$dbName = "kal";
break;
case 'om1':
$dbName = "kal1";
break;
default:
break;
}
if(empty($dbName)){
throw new Exception("The db Name is undefined");
}
$link = mysql_connect("localhost","root","");
if(!$link){
throw new Exception("'Could not connect: ' . mysql_error()");
}
mysql_select_db($dbName) or die("colud not connect to database". mysql_error());
?>