drwg89980 2015-10-27 00:48
浏览 31

根据选定的某个值基于运行时切换数据库

I have home page where i have overlay with list of cities as an hyperlink. I have different databases for each city.

So when user clicks a city on runtime the database should be selected. Again if users change the city the database should be changed.

currently I am using below approach:

index.php

<code>

if(!isset($_REQUEST['city']))
{
    $_SESSION['city'];
    $_SESSION['usercity'] = "";
}
else
{
    $_SESSION['usercity'] = $_REQUEST['city'];
    $_SESSION['usercity'];
}

</code>

db.php

<code>

if($_SESSION['usercity'] == 'pune')
{
    $dbname = 'pune_xxxx';
}
else if($_SESSION['usercity'] == 'hyderabad')
{
    $dbname = 'hyderabad_xxxx';
}
else if($_SESSION['usercity'] == 'aurangabad')
{
    $dbname = 'aurangabad_xxxx';
}
else if($_SESSION['usercity'] == 'bangalore')
{
    $dbname = 'bangalore_xxxx';
}
else
{
    $dbname = 'xxxx';
}   

</code>

For first time it works fine but if want to change the city its getting failed because of previous DB in session.

Let me know the solution or alternative to this process

Thanks advance

  • 写回答

1条回答 默认 最新

  • dsnrixf6765 2015-10-27 03:38
    关注

    You would need to again do session_start(); after you detect the change in $_REQUEST['city'] and then set the session variable.

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类