编辑:已解决
我正在尝试从单独的文件传递一些mysql连接的变量。但是,它们似乎只是以字符串形式出现而不是变量。
connect.php:
<?php
function connect(){
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
$conn1 = new mysqli($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
echo $conn1->host_info . "\n";
}
connect();
?>
DB-connect.php:
<?php
$servername = "localhost";
$username = "-";
$password = "--";
$dbname = "---";
?>
消息传来:
$servername = "localhost"; $username = "-"; $password = "--"; $dbname = "---";Connection failed: Access denied for user '-'@'localhost' (using password: NO)
注释: 最后的$ password与db-connect.php文件中的密码不同。这是我第一次尝试时使用的旧密码。此外,用户' - '@'localhost'是旧用户,而不是来自$ username的用户。
请提出任何想法,谢谢? 马特
答案 0 :(得分:0)
也许这种方式对你来说可能更好。
<?php
require ($_SERVER['DOCUMENT_ROOT'].'/db-connect.php');
function connect(){
global $servername, $username, $password, $dbname;
$conn1 = new mysqli($servername, $username, $password, $dbname);
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}
echo $conn1->host_info . "\n";
}
connect();
?>