1

I would like to display a MySQL table in the browser with PHP and created the file demo-db.php

<!DOCTYPE html>
<html>
<body>

<?php
$servername = "localhost";
$username = "fabian";
$password = "XXX";
$dbname = "music";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
else echo "everything fine";

mysqli_close($conn);
?>

</body>
</html>

When I run the file in the terminal with php -f demo-db.php I receive the following output:

<!DOCTYPE html>
<html>
<body>

everything fine
</body>
</html>

Unfortunately, when I navigate to http://localhost//demo-db.php I receive the following access denied error:

Connection failed: Access denied for user 'fabian'@'localhost'

I tried to search the web for help but to no avail. The phpinfo.php file of my machine can be seen here.

2 Answers 2

1

After some time, I managed to find one solution to the question:

A comment in a stackoverflow question about accessing a MariaDB database with PHP via a browser states ''newer versions of MariaDB / MySQL don't allow you to use the root user via PHP.'' When using a different user than the root I can indeed connect to the MySQL database as intended.

0

The issue must be with SELinux, Enable the following boolean.

setsebool -P httpd_can_network_connect_db=1

1
  • 1
    When I enter the command above, I receive the following message setsebool: SELinux is disabled . This Ask-Ubuntu question states that SELinux is not installed on Ubuntu (the OS I use) but apparmor is used instead. Do you have any ideas what I can do instead?
    – fabian
    Jun 27, 2018 at 10:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .