0

this is my script

<?php
  define('HOST','databases.000webhost.com/localhost');
  define('USER','id847165_user');
  define('PASS','qwertyuiop');
  define('DB','id847165_db');
  $con = mysqli_connect(HOST,USER,PASS,DB);

  $name = $_POST['name'];
  $address = $_POST['address'];

    $sql = "insert into Persons (name,address) values ('$name','$address')";
  if(mysqli_query($con,$sql)){
    echo 'success';
  }
  else{
    echo 'failure';
  }
  mysqli_close($con);
?>

this is my error

Warning: mysqli_connect(): (HY000/2002): Connection refused in /storage/h5/165/847165/public_html/db.php on line 6

Notice: Undefined index: name in /storage/h5/165/847165/public_html/db.php on line 8

Notice: Undefined index: address in /storage/h5/165/847165/public_html/db.php on line 9

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /storage/h5/165/847165/public_html/db.php on line 12
failure
Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /storage/h5/165/847165/public_html/db.php on line 18

screenshot of database enter image description here

can anyone help me to make it correct

hassan
  • 7,812
  • 2
  • 25
  • 36
Arun Mohan
  • 211
  • 3
  • 12
  • give host as 'databases.000webhost.com' – Naincy Feb 23 '17 at 11:19
  • You do not do _any_ error checking or handling whatsoever, you blindly trust that the connection will work and use the handle. The connection call returns `false` (a boolean...) if it fails, that is the reason for your warnings... – arkascha Feb 23 '17 at 11:19
  • @Naincy Warning: mysqli_connect(): (HY000/2002): Connection refused in /storage/h5/165/847165/public_html/db.php on line 6 Notice: Undefined index: name in /storage/h5/165/847165/public_html/db.php on line 8 Notice: Undefined index: address in /storage/h5/165/847165/public_html/db.php on line 9 Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /storage/h5/165/847165/public_html/db.php on line 12 failure Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /storage/h5/165/847165/public_html/db.php on line 18 – Arun Mohan Feb 23 '17 at 11:23
  • @Naincy sorry still facing error – Arun Mohan Feb 23 '17 at 11:30
  • @ArunMohan please see my answer – Naincy Feb 23 '17 at 11:39
  • @Naincy ya i saw your answer but its worng its not working – Arun Mohan Feb 23 '17 at 11:42
  • @ArunMohan yes arun, its saying "Network not Reachable" there is nothing wrong with code there is issue on server side might be they not allowing to connect or restricted the IPs and some other security stuff....but nothing wrong with your connection code – Naincy Feb 23 '17 at 11:45
  • @Naincy don't spam – Kiran Benny Joseph Feb 23 '17 at 11:53
  • you should learn about parameterized statements, because your code is wide open to **SQL Injection attacks** – Franz Gleichmann Feb 23 '17 at 12:09
  • @FranzGleichmann I didn't get you – Arun Mohan Feb 23 '17 at 12:10
  • @ArunMohan imagine someone posting an address like `'); DROP TABLE Persons; -- `. as soon someone does this, your Persons-table is *gone*. – Franz Gleichmann Feb 23 '17 at 12:14
  • this is a real simple thing. You're trying to connect to your host on a remote connection. If you didn't "pay" for their hosting, then you can't use a remote connection. – Funk Forty Niner Feb 23 '17 at 13:02

3 Answers3

1

if your site is hosted on 000webhost

forget your 00wh info, when you create your database, you have to give it a name, username, and password, then you'll see them in the page,
$user="";<---- User name you entered when making the database(prefixed with "a8314628_"),
$password="";<---- password you entered when making the database,
$database="a8314628_forum";<---- looks like you've got this part right,
$hostname="";<---- it will be shown where i mentioned above.,
click the MySql button, ALL the info is right there, if you've set up the db.

here's an example only:
$user="a8314628_something";
$password="password";
$database="a8314628_forum";
$hostname="mysql2.000webhost.com"

it looks like you put "databases.000webhost.com/localhost" somewhere, that's wrong

good luck

:)

PLease see this

Please Follow My step to Clear your Problem,

1) open your https://files.000webhost.com/ to upload OR add your file

2) inside public_html folder create 1 file call index.php

3) put this code inside this

    <?php
      define('HOST','localhost');
      define('USER','id847165_user');
      define('PASS','qwertyuiop');
      define('DB','id847165_db');
      $con = mysqli_connect(HOST,USER,PASS,DB);

if(isset($_POST['name'])){
      $name = $_POST['name'];
      $address = $_POST['address'];

        $sql = "insert into Persons (name,address) values ('$name','$address')";
      if(mysqli_query($con,$sql)){
        echo 'success';
      }
      else{
        echo 'failure';
      }
    }else{
echo 'Please send data in $_POST First';
}
      mysqli_close($con);
    ?>

4) close&save File

5) Run your website

Kaushik solanki
  • 438
  • 3
  • 15
1

I tried below code and it gives me error like

Failed to connect to MySQL: Network is unreachable

000webhost only lets you access the database trough it's own PHP host

You can't access it from your own computer, unless you upgrade your account (by paying). Cannot connect to database (000webhost)

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$con = mysqli_connect("databases.000webhost.com","id847165_user","qwertyuiop","id847165_db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
else {
 echo 'connected';
}
?> 
Community
  • 1
  • 1
Naincy
  • 2,953
  • 1
  • 12
  • 21
0

Use localhost as connection hostname.(Only if your file is in 000webhost server)

change 'databases.000webhost.com/localhost' to

'localhost'

enter image description here

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57