Mysql_connect problem


#1

I’m trying to connect to mysql by reading hostname, username, and password from an .inc file, but, I’m having a little difficulty.

If I connect using the following format:

$host = ‘mysql.mydomain.com’;
$user = ‘username’;
$pass = ‘password’;
$name = ‘dbname’;

$db = mysql_connect($host,$user,$pass);
mysql_select_db($name,$db);

…I get access denied error. But, If I connect using the code below:

$host = ‘mysql.mydomain.com’;
$user = ‘username’;
$name = ‘dbname’;

$db = mysql_connect($host,$user,‘password’);
mysql_select_db($name,$db);

…it works.

So, the code breaks if I use the variable $pass. I made sure that $pass has the right value with no leading or trailing spaces. Please help. Thank you.


#2

Does the value of $pass happen to change anywhere after you defined it? That’s the only thing I can think of.


#3

Nope, it does not change at all. That page is the only page where I’m using $pass. There are no other pages on my site that use that variable.


#4

I have always had success doing it like this:

[code]// initialize database variables
$hostname = “mysql.mydomain.net”;
$username = “username”;
$password = “password”;
$database = “dbname”;

// connect to database
$link = mysql_connect($hostname,$username,$password);
mysql_select_db($database);[/code]Once the link has been established, there is no need to specify the database connection when choosing a database.


Simon Jessey
Keystone Websites | si-blog


#5

I’m so sorry for troubling. I had my password wrong. :embarrased: