0

I have a problem with my config.php file which returns error like this.

PHP Notice: Use of undefined constant for M_DATABASE_SERVER, M_DATABASE_USER, M_DATABASE_PASSWORD, M_DATABASE_NAME, M_ENV_SITE_ROOT and M_ENV_SITE_URL.

I have browsed for other answers which says that the answer might lie with the adding quotes, so I added them. Example: define('M_DATABASE_SERVER',$db_host); However, it does not work.

Here's my code, I hope someone can assist. Thank you.

<?php

$db_host = "localhost";
$db_user = "myname";
$db_pass = "mypassword";
$db_name = "mydatabase";

define('M_DATABASE_SERVER',$db_host);
define('M_DATABASE_USER',$db_user);
define('M_DATABASE_PASSWORD',$db_pass);
define('M_DATABASE_NAME',$db_name);

define('M_ENV_SITE_ROOT', $_SERVER["DOCUMENT_ROOT"]);
define('M_ENV_SITE_URL', "http://".$_SERVER["HTTP_HOST"]);


//define(M_ENV_SITE_ROOT, SITE_ROOT_PLACEHOLDER);
//define(M_ENV_SITE_URL, SITE_URL_PLACEHOLDER);

// Autoload classes
function __autoload($classname)
{
require('M_ENV_SITE_ROOT'.'/include/'.$classname.'.class.php');
}

// An alternative
//array_walk(glob('./include/*.class.php'),create_function('$v,$i', 'return require_once($v);')); 

// Open database

$mysql_err = false;
$db = new sql();

if(!$db->connect('M_DATABASE_SERVER', 'M_DATABASE_USER', 'M_DATABASE_PASSWORD', 'M_DATABASE_NAME'))
{
$mysql_err = "Could not connect to database.";
}

// Represents all post and get variables

$post = new post($db);
$get = new get($db);

// Initialize current admin user

$user = new user($db);

// Initialize config object

$config = new config($db);

?>
user2192094
  • 337
  • 2
  • 6
  • 15
  • 1
    The quotes are required in the `define` but should **not** be used when you want to get the value of the constant, otherwise you just get a string. So change `if(!$db->connect('M_DATABASE_SERVER', 'M_DATABASE_USER', 'M_DATABASE_PASSWORD', 'M_DATABASE_NAME'))` to `if(!$db->connect(M_DATABASE_SERVER, M_DATABASE_USER, M_DATABASE_PASSWORD, M_DATABASE_NAME))` – Nick Jan 03 '20 at 03:15
  • Hi Nick, thanks for your response! Now the config.php file works. But I am unable to post message to my database. Do you think it is a problem with my config file or database problem. Should I ask another question instead of typing here? Thanks! – user2192094 Jan 03 '20 at 03:26
  • This question has been closed as it was just a typo issue. It would be hard to get re-opened, so I would ask a new question. You can always refer back to this one if you need to. – Nick Jan 03 '20 at 03:28

0 Answers0