-4
function getDisplayName($username) {
    $query = "SELECT `dispname` FROM `accounts` WHERE `username`='slavidodo'";
    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_assoc($result);
    return $row["dispname"];
}

I tested the codes without using a function and worked fine ( idk why don't work when using function).

Regars Ahmed.

Slavi
  • 576
  • 1
  • 3
  • 18

1 Answers1

1

Because your connection $conn is not available in your function scope. Either use global or pass the connection $conn to your function parameter.

Method(1):

function getDisplayName($username) {
    global $conn;
    $query = "SELECT `dispname` FROM `accounts` WHERE `username`='slavidodo'";
    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_assoc($result);
    return $row["dispname"];
}

Method(2):

function getDisplayName($username, $conn) {
    $query = "SELECT `dispname` FROM `accounts` WHERE `username`='slavidodo'";
    $result = mysqli_query($conn, $query);

    $row = mysqli_fetch_assoc($result);
    return $row["dispname"];
}
Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37