NameSilo

Using variables in PHP

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
SpaceshipSpaceship
Watch

gericsb

Top Member
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
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
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
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

A browser tab with a favicon next to the page title
Search members
You can now search for members by name, company, location and more. Open the search at the top of any page and choose Members. You will...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back