Dynadot โ€” .com Registration $8.99

Using variables in PHP

Spaceship Spaceship
Watch
Impact
1,431
I have defined the following constants in a file called global.php where I have used my actual database username, password etc.--I just replaced them here so nobody could see them:

<?php
// Database constants
DEFINE('DBUSER', 'mydatabaseusername');
DEFINE('DBPASS', 'mydatabasepassword');
DEFINE('DBHOST', 'localhost');
DEFINE('DB', 'nameofmydatabase');
?>

I then include the global.php file in my test page using the require_once command as seen below:

<?php
require_once('global.php');
$dbh=mysql_connect('DBHOST', 'DBUSER', 'DBPASS') or die ('I cannot connect to the database because: ' . mysql_error());
etc...........
?>

No matter how I include my constants in the connect command above, I cannot connect to my database. Now if I replace the DBHOST with "localhost" and the DBUSER with the actual name of my user etc.--all stuff I defined in the gloabl.php file--everything works fine. But--I must use the actual names in " " and not the constants like above.

I made sure my global.php page and my test page were in the same folder so its not a path issue...

So why aren't my DEFINE commands working?

Figured it out...

Couldn't have the single quotes around the DBHOST, DBUSER....etc.

Thanks anyway!
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
gericsb said:
<?php
require_once('global.php');
$dbh=mysql_connect('DBHOST', 'DBUSER', 'DBPASS') or die ('I cannot connect to the database because: ' . mysql_error());
etc...........
?>

Is your problem. You need to remove the single quotes if they are CONSTANTS.

Use this:
PHP:
<?php
require_once('global.php');
$dbh=mysql_connect(DBHOST, DBUSER, DBPASS) or die ('I cannot connect to the database because: ' . mysql_error());
etc...........
?>
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back