duanao6704 2014-08-20 01:11
浏览 38
已采纳

如何检查WP数据库中的用户角色

I am developing a script that is going to check a get request against our WP database. I have been able to get by just fine until now.

Now I need to check the role associated with the email from the get request in the database. As seen here I know the user role is stored in the wp_usermeta table in the meta_value columns.

I need to run a conditional in my script where if the user role is subscriber then I will echo out "Disabled" and if it is our custom role ntnw_member then I will echo out "Enabled".

Here is what I see in the field for the meta_value column just for an example:

a:1:{s:11:"ntnw_member";b:1;}

I just need to get the part in between the double quotes and check if it is equal to either subscriber or ntnw_member.

here is attempt at the query - Note the checkRole variable is where I am getting stuck:

/**************************************************
    Get our request and test against database
**************************************************/
if(isset($_GET["email"])) {

    $userEmail = mysqli_real_escape_string($connect, $_GET["email"]);

    //check email against database
    $checkEmail = mysqli_query($connect, "select * from wp_usermeta WHERE meta_value = '$userEmail'") or die('Error: ' . mysqli_error($connect));

    if(mysqli_num_rows($checkEmail)>0){

     while ($row = mysqli_fetch_array($checkEmail, MYSQL_ASSOC)) {

        //get the user_id from database
        $userID = $row[user_id];

        $subscriber = Array(
            'subscriber' => 1
        );

        $ntnwMember = Array(
            'ntnw_member' => 1
        );

        $checkRole = mysqli_query($connect, "select * from wp_usermeta WHERE user_id = '$userID' AND meta_value = '$ntnwMember'") or die('Error: ' . mysqli_error($connect));

        if ($checkRole){
          echo 'Enabled';
        } else {
          echo 'Disabled';
        }

    } //end while $row
} else {
    //if email address does not match anything on file, kill this script
    die('Failed: Your email address does not match our records');
} //end if if(mysqli_num_rows($checkEmail)>0){}

} //endif isset
  • 写回答

1条回答 默认 最新

  • douman6245 2014-08-20 02:05
    关注

    $checkRole would return false when there's error.

    Instead, use mysqli_num_rows to check if there's any row returned.

    $userID = $row["user_id"];
    
    $sql = "select * from wp_usermeta WHERE user_id = '" . $userID . "' AND meta_value LIKE '%ntnw_member%'";
    
    $checkRole = mysqli_query($connect, $sql) or die('Error: ' . mysqli_error($connect));
    
    if (mysqli_num_rows($checkRole)>0){
      echo 'Enabled';
    } else {
      echo 'Disabled';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)