Specify columns for PDO::FETCH_KEY_PAIR

左心房为你撑大大i 提交于 2019-12-11 20:28:59

问题


I have a table with a following columns (more than 2) in my database:
1. id
2. name
3. email

I am trying to fetch key value pairs using PDO by fetching the fetchMode to PDO::FETCH_KEY_PAIR and specifying columns id and name which works just fine

But when i try to specify name and email as the desired columns i get the result set but the key is not name but usual numeric zero based indexes

0 => some@email.com

Why?

BTW i am using laravel, here is my code:

$users = new User;
$users->getConnection()->setFetchMode(PDO::FETCH_KEY_PAIR);
return $users::get(array('name','email'));

回答1:


I was able to get the desired result by using:

User::lists('email', 'name');


来源:https://stackoverflow.com/questions/20434522/specify-columns-for-pdofetch-key-pair

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!