本文介绍了PDO准备语句执行-如何仅获取关联密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很容易解决...

This should be a simple thing to solve...

当用户登录时,我得到了以下权限:

I have got these estatements when a user logs in:

    $query = $conn->prepare("SELECT * FROM admins WHERE username = :user AND password = :pass");
    $query->bindValue(":user", $user);
    $query->bindValue(":pass", md5($pass));
    $query->execute();

现在,如果用户存在,我将从数据库中返回用户信息,但如果不存在,我将返回false ...

Now if the user exists, I return the user information from the database, but if not, I return false instead...

    if($query->rowCount() > 0) {
        $admininfo = $query->fetchAll(PDO::FETCH_ASSOC);
    } else {
        return false;
    }

但是当我var_dump变量 $ admininfo 时,我在实际数组之前得到了一个带有数字键的数组...像这样:

But when I var_dump the variable $admininfo I get an array with a number key before the actual array... like this:

array (size=1)
  0 => <---- I DONT KNOW WHERE THIS COME FROM. I USED PDO::FETCH_ASSOC
array (size=9)
  'id' => string '1' (length=1)
  'username' => string 'admin' (length=5)
  'password' => string '21232f297a57a5a743894a0e4a801fc3' (length=32)
  'permissionid' => string '1' (length=1)
  'name' => string 'Administrador' (length=13)
  'lastname' => string 'Softing' (length=7)
  'phonenumber' => null
  'cellphonenumber' => null
  'info' => null

我会将这些信息放入SESSION数组中,因此我想通过$ _SESSION ["admininfo"] ["username"]或$ _SESSION ["admininfo"] ["phonenumber"]来访问它

I will put this information inside the SESSION array, so I want to access it by $_SESSION["admininfo"]["username"] or $_SESSION["admininfo"]["phonenumber"]

但是我必须改成这样:$ _SESSION ["admininfo"] [0] ["phonenumber"].

but I have to put something like this instead: $_SESSION["admininfo"][0]["phonenumber"].

如何从键中删除那个0?谢谢!

How can I remove that 0 from the keys? Thank you!

推荐答案

您正在使用fetchAll.这将为您提供数据库行的阵列-匹配的所有行.

You're using fetchAll. This will give you an array of DB rows -- of all the rows that were matched.

while循环中使用fetch逐行浏览各个行.

Use fetch in a while loop to go through individual rows one by one.

这篇关于PDO准备语句执行-如何仅获取关联密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:18