Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2010
    Posts
    260
    Thanks
    8
    Thanked 1 Time in 1 Post

    Can’t connect to database

    Hi
    I'm trying to connect my database for the first time to a registration form but when I fill out the form I am getting this error

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

    any help is appreciated.
    register.php
    Code:
    <html>
     <body>
      <form action="verify.php" method="post" name="register">
        Username: <input type="text" name="username" />
        Password: <input type="text" name="password" />
        Email: <input type="text" name="email" />
    	<input type="hidden" name="form_submitted" value="1"/> 
      <input type="submit" />
      </form>
     </code>
    </html>
    verify.php
    Code:
    <?php
    
    mysql_connect("localhost", uj, "myPassword") or die(mysql_error());
    
    mysql_select_db("users") or die(mysql_error());
    
    if ($_POST['form_submitted'] == '1') {
    ##User is registering, insert data until we can activate it
    
    $activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
    $username = mysql_real_escape_string($_POST[username]);
    $password = mysql_real_escape_string($_POST[password]);
    
    $email = mysql_real_escape_string($_POST[email]);
    
    $sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('$username', '$password', '$email', '$activationKey', 'verify')";
    
    if (!mysql_query($sql))
    
      {
    
      die('Error: ' . mysql_error());
    
      }
    
    echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";
    
    ##Send activation Email
    
    $to      = $_POST[email];
    
    $subject = " YOURWEBSITE.com Registration";
    
    $message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";
    
    $headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .
    
        'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .
    
        'X-Mailer: PHP/' . phpversion();
    
    mail($to, $subject, $message, $headers);
    
    } else {
    
    ##User isn't registering, check verify code and change activation code to null, status to activated on success
    
    $queryString = $_SERVER['QUERY_STRING'];
    
    $query = "SELECT * FROM users"; 
    
    $result = mysql_query($query) or die(mysql_error());
    
      while($row = mysql_fetch_array($result)){
    
        if ($queryString == $row["activationkey"]){
    
           echo "Congratulations!" . $row["username"] . " is now the proud new owner of an YOURWEBSITE.com account.";
    
           $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";
    
           if (!mysql_query($sql))
    
      {
    
            die('Error: ' . mysql_error());
    
      }
    
        }
    
      }
    
    }
    ?>
    Last edited by andynov123; 02-16-2013 at 12:46 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is a sql error: Access denied for user 'uj'@'localhost' (using password: YES). It has rejected your user 'uj' at 'localhost'. It will not tell you if you're username, hostname or password is invalid, so these are what you have to verify.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Oct 2010
    Posts
    260
    Thanks
    8
    Thanked 1 Time in 1 Post
    I understand what you are saying, but I don't know what I did wrong?
    Check image out for more info on my setup
    Attached Thumbnails Attached Thumbnails Can&#8217;t connect to database-img1.jpg  

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Nothing; this hasn't a thing to do with your structure. It is your mysql user account that has been rejected and it doesn't verify that the username @ localhost with the password provided as valid.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    Incorrect:
    Code:
    mysql_connect("localhost", uj, "myPassword") or die(mysql_error());
    Presumably you mean:
    Code:
    mysql_connect("localhost", "uj", "myPassword") or die(mysql_error());
    John


  •  

    Posting Permissions

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