问题
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