Retrieving data from Single table:
// Getting db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select('*');
$query->from($db->quoteName('#__tablename')); // table name without prefix
$query->order('\'ASC\'');
$query->setLimit('10');
// Reset the query
$db->setQuery($query);
// Load the results
$results = $db->loadObjectList();
Retrieving data from multiple tables(Using Joins):
// Getting db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
// give the name of field instead of column name that you want to display in the query given below and tablename instead of table1 and table2
$query
->select(array('a.*', 'b.column_name'))
->from($db->quoteName('#__table1', 'b'))
->join('INNER', $db->quoteName('#__table2', 'a') . ' ON (' . $db->quoteName('a.columnname') . ' = ' . $db->quoteName('b.columname') . ')')
->order($db->quoteName('a.columnname') . ' DESC');
// Reset the query
$db->setQuery($query);
// Load the results
$results = $db->loadObjectList();
0 Comment(s)