I can not connect to the MySQL database with Slim

2

I'm doing a test and I want to connect to the database Tarea2 and show the table EMPLEADO but I can not connect.

My user is root , for simplicity of the task I do not have passwords

This is my code.

$app->get('/EMPLEADO', function(){
  $pdo = new PDO('mysql:host=localhost;dbname=Tarea2','root','');
  $query = "SELECT * FROM EMPLEADO";
  $statement = $pdo->prepare($query);
  $statement->execute();
  $EMPLEADO = $statement->fetchAll(PDO::FETCH_OBJ);
  echo json_encode($EMPLEADO);
});

but it shows me an error:

  

Slim Application Error The application could not run because of the following error:

     

Details

     

Type: PDOException Code: 1045 Message: SQLSTATE [HY000] [1045] Access   denied for user 'root' @ 'localhost' (using password: NO) File:   /Applications/MAMP/htdocs/tarea3/index.php Line: 52

     

This is my line 52

$pdo = new PDO('mysql:host=localhost;dbname=Tarea2','root','');

What am I failing? Thank you very much.

    
asked by Juan Abel Archila Giron 09.08.2018 в 21:27
source

1 answer

0

I understand that that user exists and can connect you in another way. In mysql you not only declare a user and password but you also establish from where you can connect.

If in your user table you have root explicitly associated with 127.0.0.1 , it is not the same as localhost , since in PDO the latter is interpreted as the connection to a socket and not a TCP connection / IP

If for example you can enter via PHPMyAdmin, verify to which hosts root is associated. You can add additional entries or put it simply authorized from %

Or simpler still, use the IP and not pass the third parameter:

$pdo = new PDO('mysql:host=127.0.0.1;dbname=Tarea2','root');

Edit: to allow root to log in from anywhere, you can edit the user:

Just change the host name to "any server". Do not touch anything else and keep.

    
answered by 09.08.2018 в 21:56