Mysqli with access denied even with correct settings

Asked

Viewed 521 times

1

I’m making an unusual mistake for myself. Next: when I try to connect via mysqli I get error return on authentication ( access denied ), and the parameters are all correct! To confirm this access normally through SSH console with the same parameters and succeed, but mysqli does not work! What could be?

NOTE: the extension is enabled in the php configuration file

The connection code is simple:

    $host = "localhost"
    $user = 'usuario';
    $password = 'senha';
    $db = ' banco';

    $mysqli = new mysqli($host, $user, $password, $db);

    if (mysqli_connect_errno()) {
        print("Conexão falhou: %s\n" . mysqli_connect_error());
        exit();

    }

The return I get is:

Warning: mysqli::mysqli(): : Access denied for user 'usuario'@'localhost' to database ' banco' in /fakepath/arquivo.php on line 14
Conexão falhou: %s Access denied for user 'usuario'@'localhost' to database ' banco'
  • 2

    Does the user have permission in this bank? Local permissions, remote access permissions, etc. Pq vc does not try to log in with root to review permissions?

  • 1

    Yes. All permissions. I checked here, Even when I do normal access, console, via ssh, I can do DDL and DML normally. mysql remote is also enabled. The only one that does not work is mysqli...

  • 4

    Probably your problem is this: http://answall.com/questions/5303/query-mysql-em-php-s%C3%B3-works locally

  • When I mention remote mysql, I say that permissions for script connection on another machine is also enabled. The mysql service is running on the server but I can connect it from another location, setting mysql remote. Ok. when I speak in ssh I say I can use with the mentioned user and the database I am trying to access by php. It just doesn’t work in PHP. I’m looking into

  • 1

    I solved the problem! Something strange that I had not yet passed... I thank you for the tips. I will post how I solved.

2 answers

1

seu código tem alguns erros:
1 - falta ; no fim da primeira linha
    correto $host = "localhost";
2 - $db=' banco'; tem espaço antes de banco
    correto $db='banco'; 

$host = "localhost"
    $user = 'usuario';
    $password = 'senha';
    $db = ' banco';
  • Thank you for the answer! In this case, this code with these parameters was just for example. IN real code, with the parameters everything is right! Thank you. And I also managed to solve

1

Problem solved! Well, noting such error, I tried to connect in different ways. My problem was always the direct connection, IE, whenever I instantiated a mysqli object passing all parameters( including the name of the bank to connect ) gave access error denied. Then I installed the object only with data of connection and user and used the function "select_db" to select after being connected. The code, with the same parameters as before, worked perfectly. It stayed that way:

$host = 'localhost';
$user = 'usuario';
$password = 'senha';
$db = 'banco';

$mysqli = new mysqli($host, $user, $password) OR DIE(mysqli_connect_error());

if ($mysqli->select_db( $banco )) {
    echo "selecionado";
    //query de teste
} else {
    exit();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.