www.webdeveloper.com
Results 1 to 5 of 5

Thread: Database connect error

  1. #1
    Join Date
    Nov 2013
    Posts
    72

    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.

    Code:
    <?php
    $con = mysqli_connect("localhost","$username","$password","$db_name");
    
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }else{
        echo "Success";
    }
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    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?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2014
    Posts
    824
    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")
    Java is to JavaScript as Ham is to Hamburger.

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

  5. #5
    Join Date
    May 2014
    Posts
    824
    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.
    Java is to JavaScript as Ham is to Hamburger.

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



Recent Articles