Page 1 of 1

Database access issues

Posted: Thu Aug 10, 2006 1:52 am
by Tay
I have a php/mysql site which works fine locally, but no longer works completely on my remotely hosted server.

On four pages, a query runs to extract db information. This displays correctly. On this page, one should be able to click on a name or photo and then another query is run and a new page is opened displaying the results from another query. However, I get the following message when I make the appropriate click:

Code: Select all

Access denied for user 'user'@'localhost' to database 'databasename'
If access was denied to this user, the other four pages would not work, but they do; they all use the same database. I have created new users, altered access rights and I still get the same error message. I am stumped completely; I do not understand how access is allowed for some pages, but not for another.

The php access include file is:

Code: Select all

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_dbConnect = "localhost";
$database_dbConnect = "databasename";
$username_dbConnect = "user";
$password_dbConnect = "password";
$dbConnect = mysql_connect ($hostname_dbConnect, $username_dbConnect, $password_dbConnect) or trigger_error(mysql_error(),E_USER_ERROR); 

?>
The chunk of code on each of the relevant pages to access the php include is:

Code: Select all

<?php require_once('dbconnect.php');
mysql_select_db($database_dbConnect, $dbConnect);
*Names changed.

As I said, the site had worked fine. I then uploaded some files which worked ok locally (but needed tweaking 'live'), and on the same day the host server 'had problems' with php file security. I assume that this problem is of my making, but as I know that other sites on this server had php pages hacked, I'm not sure that the files that I uploaded are the cause. I have now changed hosting company.

I'd be grateful if somebody could point me in the right direction as I've spent many hours seaching forums and manuals but can't get to the bottom of it, and would like to know how access rights can be fine for one page but not another.

Posted: Thu Aug 10, 2006 1:54 am
by feyd
$dbConnect isn't one of your variables. Typo?

Posted: Thu Aug 10, 2006 2:11 am
by Tay
Hi

Thanks feyd. I have edited my post above; I made a typo when cleaning up the code to put on here.

I have checked and checked again, but there are no typos locally, so the problem still stands.

Posted: Thu Aug 10, 2006 4:28 am
by volka
Tay wrote:I made a typo when cleaning up the code to put on here.
Please, no special (untested) "forum edition" of your script. Isn't the original supposed to be at least as clean as the snippet you post here? It doesn't speed up things if we have to guess wether it's a typo or the "real" error.
Tay wrote:The chunk of code on each of the relevant pages to access the php include is:
How much irrelevant code is there between those two lines of relevant code?
What does

Code: Select all

echo '<div>'; var_dump($dbConnect); echo '</div>';
mysql_select_db($database_dbConnect, $dbConnect);
print?

Posted: Thu Aug 10, 2006 4:54 am
by Tay
Thank you for replying.

The var dump results were:
resource(3) of type (mysql link)

Posted: Thu Aug 10, 2006 5:23 am
by volka
Ok, that's a dead end.

Just another question. $database_dbConnect is set in dbconnect.php, why isn't mysql_select_db called there?

Posted: Thu Aug 10, 2006 6:17 am
by Tay
Thanks.
$database_dbConnect is set in dbconnect.php, why isn't mysql_select_db called there?
I don't know why it wasn't called there. I have just tried the dbconnect.php with the mysql_select_db added, but I still get the same results.

Posted: Thu Aug 10, 2006 7:17 am
by volka
Sorry, really escapes me then.

Posted: Thu Aug 10, 2006 7:26 am
by Tay
Thank you for your help and suggestions. Thought I was going mad, but perhaps not?

If I find the answer, I'll post back.

Thanks again.

Posted: Mon Aug 28, 2006 6:12 pm
by Tay
Hello again

Just to clear this thread up; I have resolved the issue.

Within a query, I hadn't explicitly referenced a table, and this caused the 'access denied' error. What was

FROM

databasename.tablename AS testvar

needed to be

FROM

identifier_databasename.tablename AS testvar

I don't know if identifier is the correct term here.... Anyhow, all is working, although this seems a rather long-winded way to do things; especially as I don't have to add the identifier as a prefix to any other table. Still haven't figured that out.

Thanks for your help. :)