user3078244
user3078244

Reputation: 53

mysqli database functions set up

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.

Upvotes: 0

Views: 57

Answers (1)

user4035
user4035

Reputation: 23729

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'];
    }   
}

Upvotes: 1

Related Questions