0

I've been trying to write a function to draw a usernames name from the database fetch he result then return it back to the parent page but nothing I have tried works and I don't even know where to go from here. my old code that I used mysql on works perfect and was easy to put together but this mysqli and all its double parameters and stuff I cant figure it out. so basically I am asking for anyone who knows how to set one of these up.

(SELECT name FROM users WHERE username = '" .$_SESSION['username']. "')

I want to mysqli_real_escape the value if possible so if anyone knows how to set up a function to return a value back to its parent page I would much appreciate it.

1 Answer 1

1

It's very similar to using mysql_* functions:

function get_user_name()
{
    global $conn;

    $sql = "SELECT name FROM users WHERE username = ?";
    if ($stmt = $conn->prepare($sql)) {

        $stmt->bind_param("s", $_SESSION['username']);
        $stmt->execute();
        $result = $stmt->get_result();
        $data = mysqli_fetch_assoc($result);

        return $data['name'];
    }   
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.