Database Connectivity Error

Website URL

Currently not local, but its on replit. I’m using the database to then implement to another server. But first I am testing in replit.

Error Message

Displayed On Screen: **Warning** : mysqli::__construct(): php_network_getaddresses: getaddrinfo for sql107.infinityfree. com failed: No address associated with hostname in **/home/runner/how-is-this-agreat-sitenet/process.php** on line **7**

**Fatal error** : Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for sql107.infinityfree. com failed: No address associated with hostname in /home/runner/how-is-this-agreat-sitenet/process.php:7 Stack trace: #0 /home/runner/how-is-this-agreat-sitenet/process.php(7): mysqli->__construct('sql107.infinity...', 'if0_35853693', Object(SensitiveParameterValue), 'if0_35853693_db...') #1 /home/runner/how-is-this-agreat-sitenet/process.php(74): connect_to_db() #2 {main} thrown in **/home/runner/how-is-this-agreat-sitenet/process.php** on line **7**

Displayed in Console:

[Mon May 13 20:51:04 2024] PHP 8.2.0RC7 Development Server (http ://0.0.0.0:8000) started
[Mon May 13 20:51:04 2024] 172.31.196.78:44870 Accepted
[Mon May 13 20:51:04 2024] 172.31.196.78:44870 [200]: GET /process.php - Uncaught mysqli_sql_exception: php_network_getaddresses: getaddrinfo for sql107.infinityfree.com failed: No address associated with hostname in /home/runner/how-is-this-agreat-sitenet/process.php:7
Stack trace:
#0 /home/runner/how-is-this-agreat-sitenet/process.php(7): mysqli->__construct('sql107.infinity...', 'if0_35853693', Object(SensitiveParameterValue), 'if0_35853693_db...')
#1 /home/runner/how-is-this-agreat-sitenet/process.php(74): connect_to_db()
#2 {main}
  thrown in /home/runner/how-is-this-agreat-sitenet/process.php on line 7
[Mon May 13 20:51:04 2024] 172.31.196.78:44870 Closing

Other Information

The code:

<?php

require_once("config.php");

function connect_to_db() {
    global $db_host, $db_username, $db_password, $db_name;
    $conn = new mysqli($db_host, $db_username, $db_password, $db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    return $conn;
}

function handle_login($conn) {
    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["action"]) && $_POST["action"] == "signin") {
        $username = mysqli_real_escape_string($conn, $_POST["username"]);
        $password = mysqli_real_escape_string($conn, $_POST["password"]);

        if (empty($username) || empty($password)) {
            echo "Please fill in all fields.";
            return;
        }

        $sql = "SELECT * FROM users WHERE username='$username'";
        $result = $conn->query($sql);

        if ($result->num_rows == 1) {
            $row = $result->fetch_assoc();
            if (password_verify($password, $row["password"])) {
                echo "Login successful!";
                // Redirect user to dashboard or home page
            } else {
                echo "Invalid username or password.";
            }
        } else {
            echo "Invalid username or password.";
        }
    }
}

function handle_signup($conn) {
    if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["action"]) && $_POST["action"] == "signup") {
        $username = mysqli_real_escape_string($conn, $_POST["username"]);
        $password = mysqli_real_escape_string($conn, $_POST["password"]);
        $email = mysqli_real_escape_string($conn, $_POST["email"]);

        if (empty($username) || empty($password) || empty($email)) {
            echo "Please fill in all fields.";
            return;
        }

        // Check if username or email already exists
        $sql = "SELECT * FROM users WHERE username='$username' OR email='$email'";
        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            echo "Username or email already exists.";
            return;
        }

        // Hash the password
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);

        $sql = "INSERT INTO users (username, password, email) VALUES ('$username', '$hashed_password', '$email')";
        if ($conn->query($sql) === TRUE) {
            echo "Signup successful!";
            // Redirect user to login page
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

$conn = connect_to_db();
handle_login($conn);
handle_signup($conn);
$conn->close();
?>

Config.php has all the credentials and is formatted properly. What is the issue? (I added spaces in the links to avoid the error for 2 links allowed.)

You can’t do that. Any code accessing an IF database must be uploaded to IF servers. IF is a webhosting provider, not a database provider.

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.