我正在尝试将变量传递给连接到我的数据库的配置文件,echo从我的配置文件(文件2)输出用户名。
文件1:
<?php
$gUsername = $_GET['pUsername'];
include 'configs/config.php';
global $gUsername;
....
文件2:
<?php
echo $gUsername;
$servername = "myserver";
$username = "user_$gUsername";
$password = "mypassword";
$dbname = "db_$gUsername";
?>
当我回显用户名和数据库名称时,我得到了我想要的东西:
$username = user_john
$dbname = db_john
但是,DB connect无法识别这些变量:
user_ db_ Connection failed: Access denied for user 'user_'@'my_ip' (using password: YES)
已编辑:请求的数据库查询:
$gUsername = $_GET['pUsername'];
include 'configs/config.php';
global $gUsername;
//echo $gUsername;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM mytable";
$result = $conn->query($sql);