admin管理员组

文章数量:1425997

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 years ago.

Improve this question
function user_role_parameter($user_id){
    global $connection;
    $query = "SELECT * FROM  users WHERE user_id = $user_id ";
    $the_user_id = mysqli_query($connection,$query);
    while($row = mysqli_fetch_assoc($the_user_id)){
        $user_id = $row['user_id'];
        $user_role = $row['user_role'];
    }
    if($user_id > 0) {
       return $user_role;
    } else {
        return "";
    }
}
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 5 years ago.

Improve this question
function user_role_parameter($user_id){
    global $connection;
    $query = "SELECT * FROM  users WHERE user_id = $user_id ";
    $the_user_id = mysqli_query($connection,$query);
    while($row = mysqli_fetch_assoc($the_user_id)){
        $user_id = $row['user_id'];
        $user_role = $row['user_role'];
    }
    if($user_id > 0) {
       return $user_role;
    } else {
        return "";
    }
}
Share Improve this question edited Jun 28, 2019 at 13:34 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Jun 28, 2019 at 13:00 raja baniraja bani 1 2
  • 2 What is your question? Please take time to format your code and explain your question better, it will help you get a better response. – RiddleMeThis Commented Jun 28, 2019 at 13:10
  • You should not be using mysqli_query() etc. directly in WordPress. There are APIs for getting this sort of thing. You should also check the database that you're querying. There is no user_role column. – Jacob Peattie Commented Jun 28, 2019 at 13:37
Add a comment  | 

1 Answer 1

Reset to default 1

You should use WordPress functions to get the user role by user id. Please try code given below:

function user_role_parameter($user_id)
{
    $user_meta = get_userdata($user_id);

    if ( !empty($user_meta) ) {
        $user_roles = $user_meta->roles;
        $user_role = '';
        foreach( $user_roles as $role ){
            $user_role .= $role . ', ';
        }
        return $user_role;
    } else {
        return "";
    }
}

本文标签: phpget user role parameter id return type string