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


Switch Databases based on runtime based on some value selected

我有一个主页,上面覆盖了城市列表作为超链接。每个城市我都有不同的数据库。

因此,当用户在运行时单击一个城市时,应该选择数据库。同样,如果用户更改了城市,则应更改数据库。

目前我正在使用以下方法:

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>

第一次它工作得很好,但如果想改变城市,它会因为之前的DB会话而失败。

让我知道这个过程的解决方案或替代方案

感谢提前

您需要再次执行session_start();在检测到$_REQUEST['city']中的更改后,然后设置会话变量。