webdevops / php-docker-boilerplate

:stew: PHP Docker Boilerplate for Symfony, Wordpress, Joomla or any other PHP Project (NGINX, Apache HTTPd, PHP-FPM, MySQL, Solr, Elasticsearch, Redis, FTP)
https://webdevops.io/projects/php-docker-boilerplate/
MIT License
562 stars 186 forks source link

Can't connect to database from PHP #78

Closed Jacopx closed 6 years ago

Jacopx commented 6 years ago

Using this code:

<!DOCTYPE html>
<html>
<body>

<?php
  $conn = mysqli_connect("localhost", "dev", "dev");
  if (mysqli_connect_errno()) {
    die("Internal error: connection to DB failed ".
        mysqli_connect_error());
  }
  if (!mysqli_select_db($conn, "database")) {
    die("Internal error: selection of DB failed");
  }

  $sql = "SELECT * FROM Person";
  $result = mysqli_query($conn, $sql);

  if (mysqli_num_rows($result) > 0) {
      // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
        echo "<br> id: ". $row["id"]. " - Name: ". $row["name"]. " " . $row["surname"] . "<br>";
      }
  } else {
      echo "0 results";
  }
?>

</body>
</html>

I can't access to the database. It's strange because trying to access to DB from DataGrip or other software with same credentials works. I'm using MySQL5.7 and i have also try to using the port 13306 in the host part...

Any help?

Jacopx commented 6 years ago

I have solved using the IP address of the container. It was my fault.