PHP传递变量以包含文件

时间:2015-10-19 19:54:19

标签: php mysql

我正在尝试将变量传递给连接到我的数据库的配置文件,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);

0 个答案:

没有答案