<?
$conn = mysql_connect("localhost", "your_username","your_password") or die(mysql_error());
mysql_select_db('your_database', $conn) or die(mysql_error());
?>

this is the code i have on database.php, when i run register page i get a erro.

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'your_username'@'localhost' (using password: YES) in /home/a4595523/public_html/database.php on line 2
Access denied for user 'your_username'@'localhost' (using password: YES)

what should i do / change?

Dani AI

Generated

— good that the problem was the wrong database name; that exact error just means MySQL refused the connection (bad credentials, host mismatch, or missing privileges). and pointed in the right direction: confirm the account and host your server expects. For background on what the “Access denied for user 'x'@'y'” message shows and how host/user matching works, see the MySQL troubleshooting notes. (dev.mysql.com)

Quick troubleshooting checklist (most common causes):

  • Verify the exact database name and username your provider uses (shared hosts often prefix names).
  • Confirm the host you’re connecting to (localhost vs 127.0.0.1 vs a remote host) — MySQL treats them differently.
  • Test from the command line: mysql -u your_user -p -h your_host your_db to separate PHP issues from credential problems.
  • Check grants/privileges (SHOW GRANTS FOR 'user'@'host') and that the DB exists and the server is running.
  • Watch for simple typos or extra whitespace. If connection fails, the error text tells you whether a password was supplied. (dev.mysql.com)

Modernize and secure the code (important if this thread is viewed years later): the old ext/mysql functions were deprecated in PHP 5.5 and removed in PHP 7+. Use MySQLi or PDO (PDO gives a nice abstraction and works with prepared statements). Always use parameterized queries / prepared statements to stop SQL injection, and create a least-privilege DB user (avoid root). Also don’t echo raw DB errors to users — log them instead. (php.net)

Example PDO connection pattern (read credentials from environment or a config file outside the webroot):

<?php
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', getenv('DB_HOST'), getenv('DB_NAME'));
$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_EMULATE_PREPARES => false];
try {
    $pdo = new PDO($dsn, getenv('DB_USER'), getenv('DB_PASS'), $options);
} catch (PDOException $e) {
    error_log($e->getMessage());
    http_response_code(500);
    exit('Unable to connect to database.');
}
?>

Store secrets out of the code (environment variables or a secrets manager) and use safe error handling and logging. These practices reduce the chance of the same “Access denied” confusion and make the app safer and easier to maintain. (12factor.net)

Recommended Answers

All 5 Replies

That error means, that with the data provided

"localhost", "your_username","your_password"

you are not able to connect to a database. I do strongly think, that you specially changed the data for us not to see it, but if you really wrote

"localhost", "your_username","your_password"

then, you should use the real data, which would be able to connect you to your database. So, if

"localhost", "your_username","your_password"

is NOT the data, you are connecting with, make sure you spelled it correctly or whether such user exists on your database(if not - create one).

You need to replace 'your_username' and 'your_password' with the username and password for the MySQL database.

thanks, it's working now, i had the wrong database in. xD

cool....

PLEASE HOW DO I ADD EMAIL ADDRESS TO MY PHP TO ALLOW SUBMITTED FORM COME TO MY EMAIL? SEE CODE BELOW...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.