Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to create a database connection file so that multiple pages will have a constant connection to the database. I am trying to understand why one set of code works as far as linking to the database is concerned, whereas the other set of code fails. Please help me understand.

Here is the code that works:

$db_connect = mysql_connect('host_name', 'user_name', 'user_password');
    if(!$db_connect){
        die('Not connected :' . mysql_error());
    }

$db_select = mysql_select_db("dispatch", $db_connect);
    if(!$db_select){
        die('No database :' . mysql_error());
    }

I am able to establish a working connection to the server, as well as selecting a table and running a query.

Here is the code that fails (I want my code to look like this code but I do not understand why this fails to connect to the server.):

$db_host = "host_name";
$db_user = "user_name";
$db_pass = "user_password";
$db_name = "user_table";

$db_connect = mysql_connect("$db_host", "$db_user", "$db_pass") or die ("Could not connect to MySQL");

I even tried this code, and it still fails:

$db_host = "host_name";
$db_user = "user_name";
$db_pass = "user_password";
$db_name = "user_table";
$db_connect = @mysql_connect("$db_host", "$db_user", "db_pass") or die ("Could not connect to MySQL");
$db_select = @mysql_select_db("$db_name") or die ("Could not connect to database");

Please note that I have tried to switch from single quote ( '' ) to double quotes ( " " ) to no avail.

Please help me understand. Please not ask why I don't just use the code that works. I want to understand why the second sets of code DO NOT work.

Please also note that this is only my second time using Stack Overflow, and I am still trying to get the hang of it. Thanks.

Hit me back...

share|improve this question
5  
Please, don't use mysql_* functions in new code. They are no longer maintained, are officially deprecated and can be dangerous in live code. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. –  Jimbo May 16 '13 at 14:33
1  
Do you get an error message in mysql_error() when you run the non-working code? –  andrewsi May 16 '13 at 14:34
 
Yes, I get the COULD NOT CONNECT TO MYSQL error. –  rezurrektor May 16 '13 at 14:35
1  
why on Earth do you quote the variables ? –  Twisted1919 May 16 '13 at 14:38
1  
@user2335979 - you had the strings wrapped in single quotes in your mysql_connect call? They're in double quotes in your question. PHP treats strings in single quotes as literals, so it uses their actual values; if they're in double quotes, then the value of variables are interpolated instead. –  andrewsi May 16 '13 at 14:50
show 12 more comments

2 Answers

Try unquoted vars:

$db_connect = mysql_connect($db_host, $db_user, $db_pass) or die ("Could not connect to MySQL");
$db_select = mysql_select_db($db_name) or die ("Could not connect to database");
share|improve this answer
 
@xy, wow... That appears to have worked. I then added a query to select from the table, and it also worked. I wonder why is it the quotes had to be removed in order for it to work. Would this be common practice? –  rezurrektor May 16 '13 at 14:45
 
Adding the quotes in the first place would not be common practise –  Anigel May 16 '13 at 14:47
 
@Anigel, I am not sure if that was a "smart-@$$" response, but I will take that into consideration. –  rezurrektor May 16 '13 at 14:53
 
No, it was a serious comment, It is not common practice to quote variable names in that case. It would only be common practice to quote strings –  Anigel May 16 '13 at 14:55
add comment

Maybe someone is your change your phpmyadmin server Password. so please sure it's right or not.

1> open " phpmyadmin ".

2> Click on " Privilenges " tab.

3> Now see your Server Username and click on " Action " (Yellow color).

4> Now you can change your Password.

Try Your Datbase connection with new Password.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.