Sparkx
Sparkx

Reputation: 59

How can I call constants in db file?

this is my db file

config.php

define(HOST_NAME, "localhost");
define(USER, "root");
define(PASSWORD, "");
define(DATABASE, "xxx");

now i wanted to call these constants in my db file

db.php
class Database {
function db_connect() {
$conn =mysql_connect(HOST_NAME, USER, PASSWORD);
}
}
$obj_db = new Database();
$obj_db->db_connect();

but these constants are not coming in mysql_connect and its giving error

 Unknown MySQL server host 'HOST_NAME' (11001)

please help thanks

Upvotes: 0

Views: 74

Answers (2)

xdazz
xdazz

Reputation: 160833

You should have config.php been included by include 'config.php';.

There are 4 ways of doing this:

include 'config.php';
include_once 'config.php';
require 'config.php';
require_once 'config.php';

PS: You need the quotes when define the constant.

define('HOST_NAME', "localhost");
define('USER', "root");
define('PASSWORD', "");
define('DATABASE', "xxx");

Upvotes: 2

glglgl
glglgl

Reputation: 91017

With

include 'config.php';

or

require 'config.php';

?

Upvotes: 1

Related Questions