我有一个包含我的连接详细信息的包含文件。我正在尝试连接到数据库,但我收到此错误:
<b>Notice</b>: Undefined variable: servername in <b>C:\xampp\htdocs\aditya\register.php</b> on line <b>5</b><br />
<b>Notice</b>: Undefined variable: database in <b>C:\xampp\htdocs\aditya\register.php</b> on line <b>5</b><br />
<b>Notice</b>: Undefined variable: username in <b>C:\xampp\htdocs\aditya\register.php</b> on line <b>5</b><br />
<b>Notice</b>: Undefined variable: password in <b>C:\xampp\htdocs\aditya\register.php</b> on line <b>
db_info.php
<?
global $servername = "localhost";
global $username="root";
global $password="******";
global $database="test";
?>
register.php
<?php
require ('db_info.php');
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
}
我在这里做错了什么?
答案 0 :(得分:0)
所以你的db_info.php应该是这样的:
$servername = "localhost";
$username="root";
$password="******";
$database="test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
}catch(PDOException $pe){
//handle exception
}
将db_info.php文件包含在需要数据库连接的其他文件中。