Results 1 to 5 of 5

Thread: Database connect error

  1. #1
    Join Date
    Nov 2013

    Database connect error

    Can someone please help me figure this error out?

    Access denied for user 'username'@'localhost' (using password: YES)

    I just created a new database and I have the most simple database connection string that I could have seen below but when I run it, it fails with the above error. I am certain that the username, password, and database name is correct. I have also made certain to give the username all privileges. The database is at Go Daddy if that matters.

    $con = mysqli_connect("localhost","$username","$password","$db_name");
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
        echo "Success";

  2. #2
    Join Date
    Aug 2004
    Well, it's telling you that either the user name you have supplied is not a defined database user (not to be confused with a system login, in case that's the issue?), or the password you provided is not correct for that user name. So, where do $username and $password get defined, and have you tried any debugging to see if they have the correct values just before you try to connect?
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    I have a question... why are you wasting time using complex string parsing when you're just passing the values?!? Lose the quotes on the things that don't need quotes! Likewise, try to use singles not doubles unless you REALLY need it.

    $con = mysqli_connect('localhost', $username, $password, $db_name);

    You don't need quotes on those when passing them from variables. Unlikely that was the problem, but it can't be helping.

    Though I'd probably also swing an axe at the procedural style calls since they add a little overhead for nothing -- but that's why I use PDO.

    $con = new mysqli('localhost', $username, $password, $db_name);

    Also, are you SURE those variables are in scope of where you're calling them? (NOT that I'd store that stuff in global scope, but that's a whole other topic called "security")

  4. #4
    Join Date
    Nov 2013
    How in the world did I not see that before? I stupidly put quotes around the variables.

  5. #5
    Quote Originally Posted by wevie View Post
    How in the world did I not see that before? I stupidly put quotes around the variables.
    Common mistake -- I've seen that several dozen times, I've even seen it in some online tutorials. It happens.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center