1
votes

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
here it says 'ODBC'@'localhost' ,but in the connection file you said root. have you did any modification to the file ?sush
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 dataEinacio
I've created a chat room to discuss this issue, since this appears to require extended discussion. Please join me here.AgentConundrum

3 Answers

7
votes

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);
2
votes

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

0
votes

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.