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