I'm using Joomla 2.5 and I need to retrieve the field 'avatar' for the current user.
This is the code I'm using:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('avatar')
->from('#__discuss_users')
->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
But it isn't displaying any data. I tried changing the last line to:
$results = $db->loadResult();
But it doesn't work either.
If you are using it as you posted, you won't include the actual value of $id in your query. You need to append it to the string like this:
$query->select('avatar')
->from('#__discuss_users')
->where('id = '.$id);
I am usually not using the $query->select stuff, but a plain old query. Thus, you might try to do it like this (this might not be best practice though):
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDBO();
// $query = $db->getQuery(true);
$query = "SELECT ".$db->quoteName('avatar')
." FROM ".$db->quoteName('#__discuss_users')
." WHERE ".$db->quoteName('id')
." = ".$db->quote($id).";";
// $query->select('avatar')
// ->from('#__discuss_users')
// ->where('id = $id');
$db->setQuery($query);
$results = $db->loadObjectList();
Thanks for your help. The following code works:
$user = JFactory::getUser();
$id = $user->get('id');
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select($db->quoteName('avatar'))
->from($db->quoteName('#__discuss_users'))
->where('id = '.$id);
$db->setQuery($query);
$results = $db->loadResult();
echo $results;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.