Page 1 of 1

mysql_connect problem

Posted: Wed Oct 12, 2005 10:43 am
by sherpa
I'm having a little problem connecting to mysql database.

If I connect using the code below, it works without any problems.

Code: Select all

$db = mysql_connect(“db.mydomain.com”, “username”, “password”);
            mysql_select_db(“dbname”);
BUT, if I connect using the following code, it gives me an error:

Code: Select all

$host = “db.mydomain.com”;
            $user = “username”;
            $pass = “password”;
            $name = “dbname”;
            $db = mysql_connect($host, $user, $pass);
            mysql_select_db($name);


With this code, I get Warning: mysql_connect(): Access denied for user 'username'@'ike.myhost.com' (using password: YES)

Please let me know what I'm doing wrong. Thank you in advance.

Posted: Wed Oct 12, 2005 10:47 am
by Burrito
$db shouldn't work anyway.

you need to look into using the mysql_connect() function.

Posted: Wed Oct 12, 2005 10:53 am
by sherpa
Burrito wrote:$db shouldn't work anyway.

you need to look into using the mysql_connect() function.
I read through that page, but, it does not really help me at all. I'm sorry, I forgot to include mysql_connect in the code above.

Posted: Wed Oct 12, 2005 10:59 am
by Burrito
well now that you've edited your post it makes more sense 8)

the way you had it before:

Code: Select all

$db = ("host","username","password);
is what I was referring to when I said $db shouldn't work anyway.

the two ways you have it written now, for all intents and purposes, are exactly the same. One thing you might check is to make sure you're using "real" quotes and not the quotes that you posted in here. You might ensure this by using single quotes. Other than that, I'm at a loss as to why the first one works and the second doesn't.

Posted: Wed Oct 12, 2005 11:02 am
by sherpa
Just tried it with single quotes as well. Same error! Basically, what I'm trying to do here is read my host, username, and password through an .inc file and then connect using the values obtained from the file. I made sure I'm reading the right values by outputting them first. This is really weird.

Posted: Wed Oct 12, 2005 11:11 am
by Burrito
try changing your .inc file to a .php file...although I don't *think* it matters.

so your include file could be:

db.inc.php

Code: Select all

$host = "hostname";
$username = "username";
$password = "password";
$database = "database";
then your main php page would be:

index.php

Code: Select all

require_once("db.inc.php");
mysql_connect($host,$username,$password);
mysql_select_db($database);
make sure your include or require statement is above your mysql functions....

Posted: Wed Oct 12, 2005 11:12 am
by sherpa
I just realized another thing. If I connect using the code below, it works.

Code: Select all

$host = 'mysql.myhost.com';
$user = 'username';
$name = 'dbname';

$db = mysql_connect($host,$user,'password');
mysql_select_db($name,$db);
So, the code breaks only when I use $pass = 'password' and put $pass in mysql_connect statement.