My server connection is fine using the $conn statement, so I know my username and password are good.

However when I try to select the database and insert a record I get errors 1044 and 1046, access denied and no database selected, respectively. I'm baffled because I have searched many online forums and my code comes directly from GoDaddy's documentation. Any suggestions would be greatly appreciated, thank you.

Here is my code:

$conn = mysql_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
} else {
    echo "Connection successful!!! <br><br>";   // I GET THIS MESSAGE SO I KNOW MY $CONN STATEMENT IS GOOD
}

if (!mysql_select_db($dbname, $conn)){
    echo "Error# ".mysql_errno($conn) . ": " . mysql_error($conn) . "<br>"; 
// I AM GETTING THIS:  Error# 1044: Access denied for user 'MYUSERNAME'@'localhost' to database 'MYDB'
// (USERNAME AND PASSWORD CHANGED HERE TO PROTECT THE INNOCENT...)
    echo "Unable to select database <br><br>"; 
}

$sql = "INSERT INTO WelcomeEmail VALUES (NULL, '$fname', '$lname', '$email', 'N', '$regdate')";

if (mysql_query($sql, $conn)) {
    $last_id = mysql_insert_id($conn);
    echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
    echo "Error# ".mysql_errno($conn) . ": " . mysql_error($conn) . "<br>"; 
//  I AM GETTING THIS:  Error# 1046: No database selected
    echo "Unable to INSERT new record for: $fname, $lname, $email, $regdate <br>" ;
}

    First and foremost, the mysql_* functions have been deprecated for about a decade now, and removed entirely in version 7, so the code you're writing isn't going to work at all in the near future. Look into PDO - it's modern, supported, easier, and most importantly safer. Second, you don't need to pass the database name to the mysql connection string; just the server, username, and password will do. Finally, are you sure you've got the database name correct? I know it sounds like a condescending question - not meant to be. It's just one of those easy to overlook things...

      And, as well as what maxxd said above (especially the bit about PDO) - have you checked what that first error message is telling you about?

      // I AM GETTING THIS: Error# 1044: Access denied for user 'MYUSERNAME'@'localhost' to database 'MYDB'

        I'm on godaddy as well

        try{
            	   $db = new PDO("mysql:host=localhost;dbname=Your database name here", 'your username here', 'your password here ');
            	   $db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            	}  catch(PDOException $e){
            		echo $e->getMessage();
            		die();
            	}
        

        Yes localhost :rolleyes:

          Definitely stop using the mysql_* functions like they all said.

          As Weedpacket points out, the user with which you are connecting to your db server doesn't have permission to use the database you are trying to select. It's a permission problem.

            Write a Reply...