Princess Niki
Princess Niki

Reputation: 55

mysqli_connect(): (28000/1045): Access denied for user 'username'@'localhost' (using password: YES)

I get this error when i configure the database in the shared hosting server

mysqli_connect(): (28000/1045): Access denied for user 'username'@'localhost' (using password: YES)

but it is working properly in the localhost.

<?php 
session_start();



if($_SERVER['HTTP_HOST']=="localhost")
{
$serverIp="localhost";
$userName="user1";
$password="XXXXX";
$dbname="example";

}else
{


$serverIp="localhost";
$userName="username";
$password="password";
$dbname="dbname";
}

$cn=mysqli_connect($serverIp,$userName,$password) OR Die("Couldn't Connect - ".mysql_error());
$link=mysql_select_db($dbname,$cn)or Die("Couldn't SELCECT - ".mysql_error()); 
?> 

Upvotes: 2

Views: 35338

Answers (6)

Hadisur Rahman
Hadisur Rahman

Reputation: 826

You don,t need to do anything let alone changing the pass. Change it, and will surely work. Thanks

Upvotes: -1

Rami
Rami

Reputation: 79

Just change your cpanel password and it will work.

Upvotes: 8

Alvine Atsiaya
Alvine Atsiaya

Reputation: 1

I believe this i the php admin mysqli_connect error.

I found out this on my cpanel under MySQL Users No db_users.

This the admin panel. WHM in this case also has no db user.

restarting my host machine solved the problem. Then #sudo service mysql restart. Before all that resetting the password is compulsory.

Upvotes: -2

OmkieIT Solutions
OmkieIT Solutions

Reputation: 434

In most of the cases, this error can be connected with the recent change of cpanel password or migration of the server to new hosting server..

So this works fine when you connect remotely BUT when you try to access the DB via cpanel at phpmyadmin, you get this

you had the following error with php myadmin: mysqli_connect(): (28000/1045): Access denied for user 'wmct4s'@'localhost' (using password: YES) Undefined index: auth_type 

This will be solved simply by reset the password and restarted cpanel.

the command to restart cpanel is /scripts/restartsrv_cpsrvd

Upvotes: 6

Hashemi Rafsan
Hashemi Rafsan

Reputation: 300

Your mistake was you use mysql and mysqli both . if you want use mysql then you can write:

$cn = mysql_connect($serverIp, 'mysql_user', 'mysql_password');
$link = mysql_select_db($dbName,$cn);

or if you want use mysqli then you can write:

$link= new mysqli($serverIp,$userName,$password,$dbName);

Upvotes: 0

Francesco Borzi
Francesco Borzi

Reputation: 61724

it looks like you are inserting the wrong credentials in the else block, you can try to put the same credentials as the if block and check if that works

Upvotes: 1

Related Questions