Reputation: 45
I am running pdo queries on different database tables and i am getting the following object result
object(stdClass)#6 (8) {
["user_id"]=> string(2) "36"
["username"]=> string(10) "nomoinc123"
....
}
If i wanted to get the user_id i would have to write
$obj->user_id
However, i want to get user_id without having to explicitly write it so that when i do queries on other tables it would get that first Primary Key entry regardless of its name
NB: All my Primary keys are written in this format name_id
Upvotes: 2
Views: 3166
Reputation: 350252
If you can guarantee it is the first value you need (as the title of your question suggests), then reset
will do what you want:
$firstvalue = reset($obj);
But it would make more sense if you would change your SQL query to return the key with a fixed alias. For example:
select user_id as key,
/* some other fields come here */
from users
And then you would just do $obj->key
.
Upvotes: 3