joomla3.0 Database queries (select) Simple select query


Example

A simple query that selects all users from the #__users table with a username that matches John

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*');
$query->from('#__users');
$query->where('username = '. $db->q('John'));
 
$db->setQuery($query);

$results = $db->loadObjectList();

You can also chain the query to improve readability and reduce the SQL code like so:

$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select('*')
      ->from('#__users')
      ->where('username = '. $db->q('John'));
 
$db->setQuery($query);

$results = $db->loadObjectList();

Note that in this example, we have used $db->q() which is the shorthand methods for $db->quote()