我如何设置$dbname
以便随着此类中会话值的变化而变化?
无论尝试如何,我总是会出错。
class db_config
{
// default connection
public static $dbname = $_SESSION['db_to_use']; // Your database name
public static $dbuser = 'root'; // Your database username
public static $dbpass = 'xxxxxxx'; // // Your database password
public static $dbhost = 'localhost';
....
}
预先感谢
答案 0 :(得分:1)
您可以按照以下方式使用某些内容:
<?php
session_start();
$_SESSION['db_to_use'] = 'mydatabase';
class db_config
{
// default connection
public static $dbname = '';
public static $dbuser = 'root'; // Your database username
public static $dbpass = 'xxxxxxx'; // // Your database password
public static $dbhost = 'localhost';
public function __construct()
{
$this->setDbName();
}
public function setDbName()
{
if(isset($_SESSION['db_to_use']) && !empty($_SESSION['db_to_use'])) {
self::$dbname = $_SESSION['db_to_use']; // Your database name
} else {
return false;
}
}
}
$con = new db_config();
echo 'db_name : ' . $con::$dbname;