I have a file called config.php in which i defined 4 constants:
<?php
define('HOST','localhost');
define('USER','root');
define('PASSWORD','');
define('DATABASE','some_database');
?>
What I want to do is call this script with request_once function inside the other php file called database.php which will use the constant from this file for connecting
<?php
require_once('config.php');
$connect = mysql_connect(HOST,USER,PASSWORD);
$select_db = mysql_select_db(DATABASE);
?>
What i get returned is that the host,user,password and database are not defined. Why is that? This is a sample simplified code i used for connection but the essence of the problem is here. Thank you in advance.
PHP by default DO NOT SHOW Notices of undefined constants. So if you use a undefined constant PHP would simple treat that one as a string. For example,
if you uncomment above
define()PHP will printfooinstead and won’t raise a notice.Personally I would suggest you to make a bit deeper test:
0) Declare
error_reporting(E_ALL)in config.php1) Check whether file exists and it readable before you include this
2) Check whether constants are actually defined before you use them
Finally, It would like this:
File: config.php
File: dbconnection.php