-2

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\n", 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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
acesteef
  • 35
  • 1
  • 6
  • 000webhost does not allows remote connection to databases, you need to connect using `localhost` and files to be hosted on `files.000webhost.com`. btw, you may wish to hide those db credentials from this post. –  Jun 05 '18 at 06:59
  • it is really a bad approach. make some APIs and communicate with them using APIs – Abdullah Al Shakib Jun 05 '18 at 07:00
  • @Arvind I don't really get your approach with localhost and a hosted file. what exactly do you mean? – acesteef Jun 05 '18 at 07:07
  • If 000webhost allows outside connections make sure your DB-user is allowed to connect from outside and not just from localhost – brombeer Jun 05 '18 at 07:29

1 Answers1

4

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.

elki42
  • 123
  • 1
  • 10
Sudhir Sapkal
  • 1,018
  • 7
  • 14
  • I left the credentials out this code for security reasons, but i'm 100% sure the credentials are correct. – acesteef Jun 05 '18 at 07:11
  • Did you tried manually to connect database server from your web server ? by doing given command ? – Sudhir Sapkal Jun 05 '18 at 07:14
  • I did not, because I don't know how to do that. – acesteef Jun 05 '18 at 07:18
  • Ohh, From where this code is executing ? from production or local ? if local then open your command prompt and hit this command. if it is production then first you need to connect production server using ssh and hit this command from there. – Sudhir Sapkal Jun 05 '18 at 07:21
  • By looking into error that you have got it seems your web server is not able to connect to database server, Just check you are using correct host name , database name, user , port of database server. – Sudhir Sapkal Jun 05 '18 at 07:23
  • 1
    im doing this from my local server. i will try to do it using the command prompt. – acesteef Jun 05 '18 at 07:25