我有两个数据库,我想在userid密码条件下选择一个数据库,但是现在它不能处理这个条件。我怎样才能实现它?目前,只选择第一个数据库,而不是第二个数据库。
<?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());
}
?>
答案 0 :(得分:0)
如果您在脚本中使用$_SESSION
,则必须启动会话,否则该会话不可见。
If测试需要稍微修正,isset()
返回一个布尔值,而不是数组中出现的数据。
<?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());
}
?>
答案 1 :(得分:0)
试试这个:
<?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());
}
?>
答案 2 :(得分:0)
您无法将isset
与字符串进行比较,因为它返回true / false。
此外,您必须在要使用会话的文件中使用session_start()
。
修复你的代码:
<?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());
}
?>
但是这个代码的一个更好的方法是使用mysqli,因为不推荐使用mysql。
<?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);
}
}
?>
这应该有效。
如果您正在使用其他mysql函数,则第二个代码将无法正常工作,除非您将所有这些代码更改为mysqli。
例如mysql_fetch_array($result)
将成为mysqli_fetch_array($link, $result)
答案 3 :(得分:0)
不要使用 mysql _ 功能。它们已被弃用。使用 mysqli _ 或预备语句。
[注意:MySQLi扩展程序适用于MySQL 4.1.13或更高版本。]
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();
}
?>
答案 4 :(得分:0)
试试这段代码,可能会有所帮助。代码很简单 为了调试您的问题,请使用
echo print_r($_SESSION, true);
要查看您要使用的值。
<?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());
?>