How can i set $dbname
to change as session value changes in this class? i always get error whatever i try.
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';
....
}
thanks in advance
You could use something along these lines:
<?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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.