So I'm making a register page for a website. and I have to send data to that database. The problem is that the database is being hosted and I don't know how to connect to it. this is my code so far : (Note that I did leave out the password for security reasons.)
if(isset($_POST['register'])){
if(isset($_POST['voornaam'], $_POST['tussenvoegsel'],
$_POST['achternaam'], $_POST['straat'], $_POST['huisnummer'],
$_POST['postcode'], $_POST['woonplaats'], $_POST['telefoon'],
$_POST['mobiel'], $_POST['e-mail'], $_POST['wachtwoord'],
$_POST['rekeningnummer']))
{
//personal info
$voornaam = $_POST['voornaam'];
$tussenvoegsel = $_POST['tussenvoegsel'];
$achternaam = $_POST['achternaam'];
//contact info
$straat = $_POST['straat'];
$huisnummer = $_POST['huisnummer'];
$postcode = $_POST['postcode'];
$woonplaats = $_POST['woonplaats'];
$telefoon = $_POST['telefoon'];
$mobiel = $_POST['mobiel'];
//account info
$wachtwoord = $_POST['wachtwoord'];
$hash = password_hash($wachtwoord, PASSWORD_BCRYPT);
$email = $_POST['e-mail'];
$rekeningnummer = $_POST['rekeningnummer'];
//link naar remote database.
$link = mysqli_connect("databases.000webhost.com", "", "", "");
if(mysqli_connect_errno())
{
printf("Connect failed: %s
", mysqli_connect_errno());
exit();
}
$sql = "INSERT INTO users (username, password, Tussenvoegsel, Achternaam, Straat, Huisnummer, Postcode, Woonplaats, Telefoon, Mobiel, Email, Rekeingnummer)
VALUES('$voornaam', '$hash', '$tussenvoegsel', '$achternaam', '$straat', '$huisnummer', '$postcode', '$woonplaats', '$telefoon', '$mobiel', '$email', '$rekeningnummer')";
mysqli_query($link, $sql);
mysqli_close($link);
}
}
this code gives me an error. the error says :
A connection attempt failed because the connected party did not respond correctly after a certain period of time, or the connection that was made failed because the connected host did not respond
I hope someone can help me with this. I've been stuck here for some time now.
Please check this code to connect to your database.
<?php
$servername = "localhost"; // Your database server name
$username = "username"; // Your database user name which you use to connect
$password = "password"; // Your database password
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
This should solve your problem. If not then please try to connect with database server from your web server manually from terminal by doing below command.
mysql -u username -p your_dbname
After this command press enter and it will ask for password, You have to enter your password here.
If this command connects to your db then your php code should also work. Just check whether the credentials you are using are correct or not.