[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 03-02-2007, 06:17 PM   #1 (permalink)
NamePros Regular
 
Join Date: Jan 2006
Posts: 696
15.00 NP$ (Donate)

gericsb is an unknown quantity at this point


Using variables in PHP

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!
gericsb is offline  
Old 03-02-2007, 07:58 PM   #2 (permalink)
NamePros Regular
 
Join Date: Feb 2006
Posts: 581
192.30 NP$ (Donate)

psalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nicepsalzmann is just really nice


Quote:
Originally Posted by gericsb
<?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 Code:
<?php
require_once('global.php');
$dbh=mysql_connect(DBHOST, DBUSER, DBPASS) or die ('I cannot connect to the database because: ' . mysql_error());
etc...........
?>
psalzmann is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 06:55 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85