tapewormbyte Posted December 1, 2010 Share Posted December 1, 2010 I'm working offline, with apache on ubuntu. I have written my own session handler and my mysql connects, inserts and updates the session handler just fine. I'm in the process of writing a "registration" form, and when I attempt to INSERT the data after it has been validated I get the warning: mysql_connect(): Access denied for user 'root'@'localhost' (using password: YES) But, I'm already connected? Why would this happen? I'm sure if it were attempting to connect when already connected, it would tell me so. Likewise if it were a syntax error. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/220315-warning-mysql_connect-access-denied/ Share on other sites More sharing options...
requinix Posted December 1, 2010 Share Posted December 1, 2010 But, I'm already connected? Why would this happen? I'm sure if it were attempting to connect when already connected, it would tell me so. Likewise if it were a syntax error. Any ideas? Respectively: - I don't know, are you? - Because the username/password combination isn't a valid login to MySQL. - Probably not, actually. But it wouldn't be a problem. - Maybe. - Try taking the error message at face value. Quote Link to comment https://forums.phpfreaks.com/topic/220315-warning-mysql_connect-access-denied/#findComment-1141664 Share on other sites More sharing options...
tapewormbyte Posted December 1, 2010 Author Share Posted December 1, 2010 But, I'm already connected? Why would this happen? I'm sure if it were attempting to connect when already connected, it would tell me so. Likewise if it were a syntax error. Any ideas? Respectively: - I don't know, are you? - Because the username/password combination isn't a valid login to MySQL. - Probably not, actually. But it wouldn't be a problem. - Maybe. - Try taking the error message at face value. I'm sorry, this really has my head spinning... The following code works, successfully connecting to the database: if($_POST['passwd'] == $_POST['passwd2']) { $test = db_query("SELECT * FROM INVENTORY WHERE INV_ID = 1"); echo $test[0]['NAME']; $passwd = $_POST['passwd']; However if I change the code ever so slightly, it doesn't work, giving me the connection error: if($_POST['passwd'] == $_POST['passwd2']) { $passwd = $_POST['passwd']; $test = db_query("SELECT * FROM INVENTORY WHERE INV_ID = 1"); echo $test[0]['NAME']; I don't get it... EDIT: oh my god... I just realized I'm resetting the $passwd variable... Same variable I previously set for the database connection. DUH.. Thanks for the help... Quote Link to comment https://forums.phpfreaks.com/topic/220315-warning-mysql_connect-access-denied/#findComment-1141990 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2010 Share Posted December 1, 2010 It's likely that your db_query() function was written poorly and it expects the main program variable $passwd to hold your database connection password. Posting the code for the db_query() function would be the quickest way of getting help. Edit to your edit (and Dan's post): Please, please don't ever use the global keyword, ever. It causes the kind of problem you were having and surprisingly WASTES TIME both in debugging problems and in writing code because suddenly you must remember what variables you are using inside of functions. Functions are supposed to make writing code EASIER and FASTER, not take more time trying to remember what you used where. After you write a function and test it, you should not need to remember what it is doing, just what parameters it takes, what function it performs, and what result it returns. Quote Link to comment https://forums.phpfreaks.com/topic/220315-warning-mysql_connect-access-denied/#findComment-1141995 Share on other sites More sharing options...
ManiacDan Posted December 1, 2010 Share Posted December 1, 2010 This is why you NEVER use globals. Your dbconnect function should be entirely self-contained, or use global CONSTANTS (not variables) to perform its actions. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/220315-warning-mysql_connect-access-denied/#findComment-1141998 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.