PDA

View Full Version : Connect to MySQL using variables($dbhost, $dbuser, $dbpass)?



maida
11-02-2004, 04:49 PM
From http://forums.westhost.com/phpBB2/viewtopic.php?t=1983

Here's another thing you can try... Change this line:


$db = mysql_connect ($dbhost, $dbuser, $dbpass)

to:


$db = mysql_connect ('localhost', 'root', 'pass')

The reason I suggest that is because I remember a while ago some code of mine refused to connect to the database when the details were passed to mysql_connect as variables: they had to be declared as constants, or "hard coded", or I had to explicitly set the variables to be global.

Let us know how it works out.

So this fixed a problem we were having. Thanks FZ! :D

Did you ever figure a way to connect using variables? Or will we just have to hard code it each time? :?

Maida

jalal
11-03-2004, 02:04 AM
This is strange.... all my scripts connect using variables!

I suspect that the problem is the scope of the variables, if the variables are created in one scope and are not visible in another then they will be blank. Otherwise there is no reason the mysql_connect would even know if they were constant or a variable.

FZ
11-03-2004, 10:50 AM
I think jalal is right... I declare $dbhost, $dbuser, $dbpass at the top of my script, and then use the global keyword when I use them to mysql_connect (which just happens to be in its own function). I think defining these three as constants should also work. It's funny - in some setups having them as variables is not a problem, and in others it is...

maida
11-05-2004, 01:36 AM
Thanks for the replies...I'll let you know how things work out!

Maida