我已经在本地机器上安装了WAMP服务器,我已经编写了php脚本将记录插入数据库,但我得到以下错误-
注意:使用未定义的常量DB_SERVER -假设'DB_SERVER'在C:'wamp'www'testrongervices'db_connect.php on line 32
以下是我的db_config代码 <?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password
define('DB_DATABASE', "test_db"); // database name
define('DB_SERVER', "localhost"); // db server
?>
Here is my **db_connect.php code**
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT
{
// constructor
function __construct()
{
// connecting to database
$this->connect();
}
// destructor
function __destruct()
{
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect()
{
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close()
{
// closing db connection
mysql_close();
}
}
?>
请帮. .谢谢你. . !
我认为你没有显示正确的文件。问题在db_connect.php中。该文件很可能不包含db_config.php,即:
require_once 'path/to/db_config.php';
编辑:db_config.php实际上生活在同一个目录下的db_connect.php?您的代码表明它是正确的,这可能是不正确的?
编辑:如果你把$var = 'hello'在db_config.php中,然后在db_connect.php中打印$var,你看到'hello'了吗?只是为了澄清你正在连接的文件…