简体   繁体   中英

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());

}
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM