-1

I am trying to access database by mysql procedure. It produces the following error. I know that array key have to define. I have done it but why it produces ..............

Use of undefined constant BLOG - assumed 'BLOG'

here is my code:

$config['db_host'] = 'localhost';
$config['db_user'] = 'root';
$config['db_pass'] = 'root';
$config['db_name'] = 'blog';

foreach ($config as $k => $v) {
    define(strtoupper($k), $v); 
}

mysql_select_db(BLOG);

I don't know what is the problem here.Any help will be appreciated.Thanks

sujan mridha
  • 89
  • 2
  • 10

2 Answers2

5

You are defining the keys as the constant name and you are using the value to use it.

It should be -

mysql_select_db(DB_NAME);

The constants are -

DB_HOST
DB_USER
DB_PASS
DB_NAME
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

First You have to Connect to Database in order to select db. MYSQL extension is deprecated try using mysqli extension instead.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}
justrohu
  • 595
  • 3
  • 9