foucquet Posted August 19, 2010 Share Posted August 19, 2010 I am slightly puzzled when I attempt to connect to MySQL using variables, if I use this:- // Connect to MySQL and select dbase mysql_connect("$host", "$user", "$pwd") or die("Computer say NO!"); mysql_select_db("test_site") or die("I've just had a senior moment, and cannot connect to the database..."); I get this error:- Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\wamp\www\php\test site\regproc.php on line 24 Computer say NO! Whereas if I use this:- // Connect to MySQL and select dbase mysql_connect("localhost", "root", "my password") or die("Computer say NO!"); mysql_select_db("test_site") or die("I've just had a senior moment, and cannot connect to the database..."); it works perfectly. Most of the tutorials I have read seem to indicate that one can use variables in this case, so I do not understand why it doesn't work... Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/ Share on other sites More sharing options...
hcdarkmage Posted August 19, 2010 Share Posted August 19, 2010 Try it without the "" around the variables: mysql_connect($host, $user, $pwd) or die("Computer say NO!"); mysql_select_db("test_site") or die("I've just had a senior moment, and cannot connect to the database..."); Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101254 Share on other sites More sharing options...
foucquet Posted August 19, 2010 Author Share Posted August 19, 2010 Nope! That makes absolutely no difference, still get the same error message Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101260 Share on other sites More sharing options...
PFMaBiSmAd Posted August 19, 2010 Share Posted August 19, 2010 It's fairly clear that your $pwd variable does not contain your exact password. Perhaps you have a space or a spelling error. Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101286 Share on other sites More sharing options...
foucquet Posted August 20, 2010 Author Share Posted August 20, 2010 It's fairly clear that your $pwd variable does not contain your exact password. Perhaps you have a space or a spelling error. Just done an extensive check for the second time, and that is all OK, but the problem is still there - I can think of a work around, but it is VERY frustrating and I would prefer this method if I can get it to work. BTW I am working on my own computer not a hosted environment. Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101543 Share on other sites More sharing options...
PFMaBiSmAd Posted August 20, 2010 Share Posted August 20, 2010 There's probably a half-dozen reasons why your $pwd variable might not have the expected value in it. Have you echoed it right before the mysql_connect() statement to see what is in it? If you want someone in a forum to tell you why your $pwd variable is not working, just post your code to get a direct answer without playing a game of 20 questions. You are getting tripped up on some vary basic programming. There are litterally millions of php web sites using variables that do work. You either have a typo in the variable name or in the value you are assigning to that variable or you are clearing that variable in your code... Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101617 Share on other sites More sharing options...
foucquet Posted August 21, 2010 Author Share Posted August 21, 2010 Yes, I did echo the variables at the start of the file, as I always do to ensure that what I expect to be in them is what is actually in them, and everything was, and is as it should be. However last night I deleted my config file and started again from scratch, testing each stage step by step, and it now seems to work fine. The weird thing is that it is an identical file to the one deleted, that is except for a couple of added details... Quote Link to comment https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/#findComment-1101997 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.