如果我要回显此查询的结果:
//Find membertype from community builder
$db->setQuery('Select cb_membertype From #__comprofiler Where id='.$user->id);
$membertype = $db->loadResult(); //load the result from the query
我会使用:
echo "Your member type is: $membertype";
我不想对每个变量思想都使用新的查询,因为它们都在同一个表中。
我宁愿运行这样的单一查询:
//Find all 3 from one query
$db->setQuery('Select cb_membertype, cb_2015vopac, cb_2015llf
From #__comprofiler Where id='.$user->id);
$result = $db->loadResult(); //load the result from the query
使用单个$result变量时,如何从该查询中回显特定字段?
首先,让我说应该引用您的值/列/表名。
其次,如果要获得多个值,则需要使用loadObjectList
而不是loadResult
。
以下将为您完成:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName(array('cb_membertype', 'cb_2015vopac', 'cb_2015llf')))
->from($db->quoteName('#__comprofiler'))
->where($db->quoteName('id') . ' = '. $db->quote($user->id));
$db->setQuery($query);
$results = $db->loadObjectList();
foreach ($results as $result)
{
echo $result->cb_membertype;
echo $result->cb_2015vopac;
echo $result->cb_2015llf;
}
所以loadObjectList
给你一个对象,然后你可以使用foreach
循环来循环它。
希望这能帮助