Search code examples
phpmysqlapachenetwork-programmingwebserver

Connect MYSQL to webserver


I have a web server running on a raspberry pi model B, i have port forwarded port 80 for http and this is successfully working. However, because there is a login page on my site, i need mysql. How do i connect my current site using php to a mysql date base, will i need to port forward the mysql port?

i've pasted the code below:

<?php 
/*PHP for connecting website to database*/

$servername = "ip/hostname";
$database = "database";
$username = "username";
$password = "password";

$conn = mysqli_connect($servername, $username, $password, 
$database);
if (!$conn) {

die("Connection failed: " . mysqli_connect_error());

}
?>

Solution

  • No, you will not need port forward. It's not the user who is logging in into mysql database, it's the php server doing the connection. As a server hostname in your php file you can simply use localhost, if the mySQL db and php server are on same device/machine.