Mysqli with access denied even with correct settings

1

I'm having an unusual error for myself. Next: When I try to connect via mysqli, I get a failed authentication return (access is denied), and the parameters are all correct! To confirm this access normally via SSH console with the same parameters and I succeed, but mysqli does not work! What could it 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 payback I have 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'
    
asked by anonymous 20.11.2016 / 22:24

2 answers

2

Problem solved! Well, looking at this error, I tried to connect in different ways. My problem was always the direct connection, that is, whenever I instantiated a mysqli object passing all the parameters (including the name of the database to connect) gave access error denied. So I instantiated the object only with connection data and user and I used the function "select_db" to select after being connected. The code, with the same parameters as before, worked perfectly. It looks like this:

$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();
}
    
21.11.2016 / 00:00
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';
    
20.11.2016 / 23:27