I'm currently working on a project and I thought about writing a function to fetch particular data from users table, and so I did, but I'm not sure is this the safest way or not ?
function getuserData($id, $field){
    global $connection;
    $sql = "SELECT `$field` FROM users WHERE id = ?";
    $stmt = mysqli_prepare($connection, $sql);
    mysqli_stmt_bind_param($stmt, "s", $param_id);
    $param_id = $id;
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    while($row = mysqli_fetch_row($result))
    {
        echo $row[0];
    }
}
getuserData($_SESSION['id'], 'name');
Is there a different way to do this, and mainly, is it safe to do it this way ?
