i get error mysqli_connect(): (28000/1045): Access denied for user ‘xxxxx’@‘localhost’
(using password: YES) in /home4/djboziah/public_html/registration/server.php on line 11
here is my code from line 11 $db = mysqli_connect('localhost', 'xxxxx', 'xxxxxx', 'dbname');
i have a user assigned to the databse name with full rights.
Are you connecting to the correct port for the database server?
I only ask that because I recently changed development PC, downloaded and installed the latest WAMP server, picked all the defaults, and because of that I have to specify an alternate port in my database connection, otherwise it defaults to MariaDB instead. The key thing, though, is that it reports that as an access problem which led me down a path of trying all sorts of permission settings before I found the issue.
Also, is localhost
correct for your host?
This is on my website hosting cpanel phpmyadmin not the my local wamp server. I am using hostgator for my hosting service.
HG’s website identifies that their port is the standard.
It does however say they’ve noticed a problem with adding IP’s to a login causing the login to lose permissions. See the bottom section here, and try what it says:
https://www.hostgator.com/help/article/how-to-connect-to-the-mysql-database
I added a IP to Remote MYSQL could that be what they mean by adding IP;s to a login?
yep. Did you follow the steps at the bottom, and did it fix your problem?
Yes i did, re added user to database, removed and then readded full privileges. same error, and no data is entered into database.
Do you have multiple users that might match this login attempt? (specifically, wildcard values)
Try this, out of curiousity:
var_dump(($db->query("SELECT USER(),CURRENT_USER();"))->fetchAll());
i am confused on select user and current user…
this is how it should look
<?php
session_start();
// variable declaration
$username = "";
$email = "";
$errors = array();
$_SESSION['success'] = "";
// connect to database
var_dump(($db->query("SELECT USER(),CURRENT_USER();"))->fetchAll());
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location:index.php');
}
}
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location:index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
oops i get a syntax error expected —> (T_OBJECT_OPERATOR)
This isnt REPLACING your $db definition. It should come after it.
s’what i get for trying to do it all in one line for ease of cleanup I guess.
$res1 = mysqli_query($db,"SELECT USER(),CURRENT_USER();");
var_dump(mysqli_fetch_all($res1));
do i enter my database user name in the SELECT_USER or CURRENT_USER?
i get on error on page. This page isn’t working
www.playkenyamusic.com
is currently unable to handle this request.
HTTP ERROR 500
Neither. You… put the words that i typed into your code. Underneath the line that starts $db =
like this, see below
<?php
session_start();
// variable declaration
$username = "";
$email = "";
$errors = array();
$_SESSION['success'] = "";
// connect to database
$db = mysqli_connect('localhost', 'XXXXX', 'XXXX', 'djboziah_registration');
$res1 = mysqli_query($db,"SELECT USER(),CURRENT_USER();");
var_dump(mysqli_fetch_all($res1));
// REGISTER USER
if (isset($_POST['reg_user'])) {
// receive all input values from the form
$username = mysqli_real_escape_string($db, $_POST['username']);
$email = mysqli_real_escape_string($db, $_POST['email']);
$password_1 = mysqli_real_escape_string($db, $_POST['password_1']);
$password_2 = mysqli_real_escape_string($db, $_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($username)) { array_push($errors, "Username is required"); }
if (empty($email)) { array_push($errors, "Email is required"); }
if (empty($password_1)) { array_push($errors, "Password is required"); }
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1);//encrypt the password before saving in the database
$query = "INSERT INTO users (username, email, password)
VALUES('$username', '$email', '$password')";
mysqli_query($db, $query);
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location:index.php');
}
}
// ...
// LOGIN USER
if (isset($_POST['login_user'])) {
$username = mysqli_real_escape_string($db, $_POST['username']);
$password = mysqli_real_escape_string($db, $_POST['password']);
if (empty($username)) {
array_push($errors, "Username is required");
}
if (empty($password)) {
array_push($errors, "Password is required");
}
if (count($errors) == 0) {
$password = md5($password);
$query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) == 1) {
$_SESSION['username'] = $username;
$_SESSION['success'] = "You are now logged in";
header('location:index.php');
}else {
array_push($errors, "Wrong username/password combination");
}
}
}
?>
Yup. Just like that. It should dump an array onto the screen when you visit the page.
This is what i get
This page isn’t working
www.playkenyamusic.com
is currently unable to handle this request.
HTTP ERROR 500
It’s… possible your database engine is locking you out of those functions, which is annoying, because it doesnt let you verify that you’re connected as the user you think you are.
At this point, if you’re 100% sure you’ve spelt everything correctly, remove the two lines i gave you, and put a ticket into HostGator support saying your code isnt letting you have access to your own database.