deadendstreet Posted April 15, 2014 Share Posted April 15, 2014 So I figured out the form issues I was having (basically, I'm allowing users to add/edit information to a database). It's been working great. Then I did some reading on PDO's. The first step I did was update my db-connect.php code.Now I get a Access denied for user 'user'@'localhost' (using password: NO) error message. Thinking I typed something wrong, I literally copied and pasted the database name, etc. from the earlier version of the file., but it still doesn't work. What would be causing this? I literally followed step-by-step directions from a website I found and in the comments section everyone said it was working. <?php $host = 'localhost'; $dbname = 'name'; $user = 'user'; $pass = 'pass'; try { $DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); } catch(PDOException $e) { echo $e->getMessage(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287792-conecting-to-db-using-pdo/ Share on other sites More sharing options...
mac_gyver Posted April 15, 2014 Share Posted April 15, 2014 is that all the code or do you have other code later in the file? that error is typical of a mysql_query() or other mysql_ function running when there isn't a mysql_connect() database connection and it (the mysql_query() statement) tires to make a connection using default credentials, which are rarely set on any server. Quote Link to comment https://forums.phpfreaks.com/topic/287792-conecting-to-db-using-pdo/#findComment-1476248 Share on other sites More sharing options...
deadendstreet Posted April 16, 2014 Author Share Posted April 16, 2014 Nope, that's the code. The tutorial I found showed that to be the only code (in that file). Let me ask this, do you know of a good PDO tutorial that you'd recommend? The ones I've found have been somewhat confusing...granted, I've been under the weather for the past week. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/287792-conecting-to-db-using-pdo/#findComment-1476284 Share on other sites More sharing options...
mac_gyver Posted April 16, 2014 Share Posted April 16, 2014 (edited) the PDO documentation at php.net is the best starting place. there's nothing technically wrong with the posted code and works for me. the error message mentions NO password, that indicates in your actual code that $pass was empty. the error message in general either means that the database name doesn't exist, with that exact spelling/capitalization, or the user/password combination doesn't exist or hasn't been given permission for the database name being used. if you copied any portion of that from some web site, you may have non-printing characters as part of it that is messing with the values. posting it here could have removed those characters, so copy/pasting it from this forum post may result in working code while your original code won't work. do you have php's error reporting set full on (error_reporting set to E_ALL and display_errors set to ON) as that may help with things like non-printing characters/character set issues in the $pass variable? Edited April 16, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/287792-conecting-to-db-using-pdo/#findComment-1476285 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.