1) Getting the default database connection:
$db =& JFactory::getDBO();
2) Set the query. Can pass in start and length LIMIT values to shrink the resultset. This will add LIMIT 0,3 to the end.
$db->setQuery($query,0,3);
3) Execute the query. You can call various methods depending on what amount of data is being returned
$db->loadObject() – returns first row as an object. e.g. $obj->field. $db->loadObjectList() – returns resultset as an object. For multiple records. $db->loadResult() – returns first field of first row as a value. A single value $db->loadRow() – returns the first row as an indexed array. $db->loadAssoc() - returns first row as an associated array. $db->loadAssocList() - returns resultset as an associated array. For multiple records. $db->Execute($sql) - Execute sql that doesn't return anything. Pass sql as parameter.
Looping through a resultset:
$db->setQuery($query,0,3);
$rows = $db->loadObjectList();
foreach($rows as $row)
{
echo $row->fldname;
}
This will create a resultset object with specified field as key index value.
$db->setQuery("SELECT category_id, name FROM category");
$categories = $db->loadObjectList('category_id');
Can you help me with the syntax to input this or similar in a form field?
$sql = “SELECT `title` FROM `jos_usergroups` WHERE 1 LIMIT 0, 30 “;
I am using j2.5.4