dongxie1907 2016-12-14 16:25
浏览 33
已采纳

如何更改查询的数组方案结果?

class user{

protected $permissions;

public function can( $permission )
    {
        if (!isset($this->permissions)) {
            /*
             fetch permissions for this user from the database, and set the
             User::permissions property, to allow caching.
             */
            $cnx = $this->connexion();
            $stmt = $cnx->prepare('SELECT permissions.permission_name FROM permissions, users_permissions WHERE users_permissions.user_id = ? AND users_permissions.permission_id = permissions.permission_id');
            $stmt->bind_param('s', $_SESSION["user_id"]);
            $stmt->execute();
            $result = $stmt->get_result();

            $people = array("Peter", "Joe", "Glenn", "Cleveland");
            var_dump($people);
            $this->permissions= $result->fetch_all(MYSQLI_ASSOC);    

            var_dump($this->permissions);    
        }

        return in_array($permission, $this->permissions);
    }
}

(scheme 1) the result of the array looks like this :

array (size=8)
  0 => 
    array (size=1)
      'permission_name' => string 'Peut créer une permission' (length=26)
  1 => 
    array (size=1)
      'permission_name' => string 'Peut modifier une permission' (length=28)
  2 => 
    array (size=1)
      'permission_name' => string 'Peut voir des comptes utilisateurs' (length=34)

i tried fetch_array instead of fetch_all but it returns only one row

(scheme 2) is there anyway to make that array look like this one because if it does it will work :

array (size=4)
  0 => string 'Peter' (length=5)
  1 => string 'Joe' (length=3)
  2 => string 'Glenn' (length=5)

instantiate a user

$that_guy = new user();
if ( $that_guy->can('Peut créer une permission')) {
  echo"yay";
}else {
  echo"you can't";
}

is there any possible way to change the array of scheme 1 and make it look like array of scheme 2 ?

  • 写回答

3条回答 默认 最新

  • dt3358 2016-12-16 12:08
    关注

    Loop through the array

    public function can( $permission )
            {
                if (!isset($this->permissions)) {
    
                    $cnx = $this->connexion();
                    $stmt = $cnx->prepare('SELECT permissions.permission_name FROM permissions, users_permissions WHERE users_permissions.user_id = ? AND users_permissions.permission_id = permissions.permission_id');
                    $stmt->bind_param('s', $_SESSION["user_id"]);
                    $stmt->execute();
    
                    $result = $stmt->get_result();
                    $stmt->bind_result($permission_name);
                    $stmt->store_result();
    
    
                    $this->permissions = array();
                    while(($row = $result->fetch_assoc())) {
                        $this->permissions[] = $row['permission_name'];
                    }
    
    
                }
    
                return in_array($permission, $this->permissions);
            }
    

    the result will look like what you want in (scheme 2)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法