gpt4 book ai didi

php - 根据所选的某些值根据运行时切换数据库

转载 作者:行者123 更新时间:2023-11-29 21:54:00 25 4
gpt4 key购买 nike

我的主页上有城市列表作为超链接。我对每个城市都有不同的数据库。

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

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

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>

第一次它工作正常,但如果想更改城市,它会因为 session 中以前的数据库而失败。

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

提前致谢

最佳答案

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

关于php - 根据所选的某些值根据运行时切换数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com