1

Warning for my php file..

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Program Files\xampp\htdocs\shadaab\register.php on line 25

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in C:\Program Files\xampp\htdocs\shadaab\register.php on line 25 Access denied for user 'ODBC'@'localhost' (using password: NO)

My Connection File:

$localhost='localhost';
$username='root';
$password='';
$dbname='school';

$con=mysql_connect('$localhost','$username','$password');
mysql_select_db('$dbname',$con);
3
  • 1
    here it says 'ODBC'@'localhost' ,but in the connection file you said root. have you did any modification to the file ? Commented Aug 21, 2011 at 4:11
  • the sample code is bad copied. there's no way it will work with single quoted strings to passa variable. please post the actual code, just replace the sensitive data Commented Aug 21, 2011 at 4:14
  • I've created a chat room to discuss this issue, since this appears to require extended discussion. Please join me here. Commented Aug 21, 2011 at 4:52

3 Answers 3

7

Final Update:

I opened a chat session with Aditii and we did some debugging there.

As it turns out, the problem was that she had upgraded from a server which had short tags enabled to one that did not. As a result, all of her code that was enclosed in short tags was not being executed, and this included her configuration file.

The error in the question was due to the mysql_connect() function not being executed because that whole file was not being executed. A mysql_query() call that relied on that connection failed because of this.

After correcting her short tags to use the full <?php tag, that file executed. As discussed in my original answer to this question, the single quotes around the variables being passed to mysql_connect() did end up causing problems. After advising that those single quotes be removed, the connection succeeded and the problem was resolved.

The entire debugging session, for any interested parties, can be found here.


First Update:

After looking at this a bit closer, there are two pieces that don't fit my original response (which I've kept at the bottom of this answer, since it still has some good info in it).

First, the error is being triggered by mysql_query() not mysql_connect(). This is probably because there is no error handling surrounding the connection, so the error doesn't get noticed until later. Try adding some error handling around the connection:

if (!($con = mysql_connect('$localhost','$username','$password'))) {
  echo mysql_error();
}

if (!mysql_select_db('$dbname',$con)) {
  echo mysql_error();
}

In the above, errors will be displayed as soon as the error occurs.

The second thing I see is that you're connecting to localhost with the ODBC user. This makes no sense if the error is being triggered by the connection code you've shown above. Either you would be logging on with a user called $username if your problem is the single quote issue I explain below, or root if you're not.

It looks like PHP is trying to connect with the default credentials for your PHP setup (found in your php.ini file), rather than the credentials you've supplied. This leads me to believe that the lines you're showing us aren't actually the lines being executed. Are you sure that you're not trying to connect elsewhere in your code? If you are, I suspect that your error lies in that portion of the code.

It's also possible (I would have to check) that when you're attempting to run mysql_query() against a null connection, maybe PHP is trying to create a connection for you. I wouldn't count on that though.

I suggest you try running the updated code I've provided above, which uses the mysql_error() function to show errors as they happen. You could even add in some echo statements of your own in those blocks, for some good old fashioned printf debugging.


Original Answer:

The problem is that you're wrapping your variables inside single quotes. If you wrap them in single quotes, PHP doesn't evaluate the variable to the value. For example, rather than passing root as the second argument, you're passing $username. In the end, you're trying to log into a server called $localhost with a user called $username with a password of $password.

Take a look at the PHP documentation page for Strings. It explains this:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

If you want to use variables inside of a string, you need to use double quotes, not single. In this case, however, you don't even need to wrap these variables in quotes at all, because their values are already strings. You can just pass the variable all by itself. Try this:

$con=mysql_connect($localhost, $username, $password);
mysql_select_db($dbname, $con);
Sign up to request clarification or add additional context in comments.

2 Comments

I have uploaded the above code. But Still problem is not solved. Even I checked privileges in phpmyadmin but same for root password is null. How can I edit php,ini file,I mean where it is?
@Aditii There's no need to edit php.ini if you're supplying credentials directly. If you modified your code to include my mysql_error() blocks, can you tell me if the error message changed at all (even if it's just the line number, etc.). Did you put echo statements in that block as well? Can you tell me if they executed? Maybe try putting an echo statement before your mysql_connect() call, to see if your code is even executing that command at all?
2

You're trying to connect to MySQL without a password; try putting it in.

1 Comment

I think that Sush in one of the above comments has the answer perhaps.
0

Does the user have permissions to execute whatever the query is on the tables that it is trying to access? I realize it's root, but for wahtever wacky reason, it may have permissions to access the DB, but not have permissions to execute SELECT/INSERT/UPDATE/whatever type queries on it's tables, or for certain tables.

2 Comments

I have checked privileges for all users in phpmyadmin and root have all the privilges.. I am also connection using that but can't. Though everything was going well but as today I installed newer version of xamp problem arises.
If its' the new version of xamp, try rolling back to last working version?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.