I am making an under construction page which is hosted on my Synology NAS.
Visitors can subscribe with their email and get informed when the website will be available.
I have trouble with the database and PHP code that add the email to the database.
If the server name is localhost
, I get the following error:
SQLSTATE[HY000] [2002] No such file or directory
When it is 127.0.0.1
or 127.0.0.1:3306
, I get the error below:
SQLSTATE[HY000] [2002] Connection refused
I didn't find the solution yet on Stackoverflow.
Here the PHP code:
<?php
$servername = "localhost";
$username = "id";
$password = "password";
$dbname = "dbname";
try {
$conn = new PDO( "mysql:host=$servername;dbname=$dbname", $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sql = "INSERT INTO email ( email ) VALUES ( '$email' )";
$conn->exec( $sql );
echo "New record created successfully";
}
catch( PDOException $e )
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
Why am I getting this error?