Jump to content

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/211186-mysql-connection-problem/
Share on other sites

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.

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...

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.