1

I'm having a problem running php code. The error only appears for this php file, other simple ones work fine. I'm using xampp. apache and mysql running green.

<?php
  $username = filter_input(INPUT_POST, 'username');
  $password = filter_input(INPUT_POST, 'password');
  if (!empty($username)){
    if (!empty($password)){
      $host = "localhost";
      $dbusername = "root";
      $dbpassword = "";
      $dbname = "test";
      // Create connection
      $conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
      if (mysqli_connect_error()){
        die('Connect Error ('. mysqli_connect_errno() .') '. mysqli_connect_error());
      }
      else{
        $sql = "INSERT INTO account (username, password)
        values ('$username','$password')";
        if ($conn->query($sql)){
          echo "New record is inserted sucessfully";
        }
        else{
          echo "Error: ". $sql ." ". $conn->error;
        }
        $conn->close();
      }
    }
    else{
      echo "Password should not be empty";
      die();
    }
  }
  else{
    echo "Username should not be empty";
    die();
  }
?>
barbsan
  • 3,418
  • 11
  • 21
  • 28

2 Answers2

0

When you are using Object-oriented mysqli connection then you have to use all Object-oriented method.

In below code you used procedural way:

if (mysqli_connect_error)
{
   die('Connect Error ('. mysqli_connect_errno.') '. mysqli_connect_error);
}

Replace with :

if ($conn->connect_error)
{
   die('Connect Error ('. $conn->connect_errno.') '. $conn->connect_error);
}

Use 127.0.0.1 instead of localhost

Ashu
  • 1,320
  • 2
  • 10
  • 24
  • The error went away, but got this now "Fatal error: Call to undefined method mysqli::connect_error() in D:\XAMPP\htdocs\example\firstsignupcode.php on line 12". Thanks for the help btw – Bardo Perez Mar 07 '19 at 21:05
  • remove function () ie. use connect_error instead of connect_error() – Ashu Mar 08 '19 at 04:57
0

Finally got the solution to this problem, I'm sharing it because I can't find a solution anywhere else. The $host should be linked to "127.0.0.1" but for an unknown reason my host only worked when it was "127.0.0.1:3307" or the name of your port for mysql at the end, don't forget the ":". PS I changed my db connection back to using the procedural way despite what the Ashu said and it worked.