To extract records from a table of Joomla! database you need to execute a SQL SELECT statement:
1) Obtain a reference to the global database object
$db =& JFactory::getDBO();
2) Prepare the SQL statement
Let's call our example table jos_mytable
$sql = "SELECT * FROM #__mytable";
$db->setQuery($sql);
setQuery() automatically replaces #__ with the table prefix set in Joomla! configuration (default prefix is jos_).
3) Execute the query and get the resultset
$results = $db->loadObjectList();
loadObjectList() returns the resultset in an array of objects where each field's value can be accessed as object property. Here is how you can cycle through results.
if(count($results)) {
foreach($results as $r) {
echo $r->field1;
echo $r->field2;
}
} In this example we assume that our table jos_mytable has 2 fields named field1 and field2.
As loadObjectList() returns null if the query fails (for a syntax error or any other reason) and count(null) returns zero, the foreach loop will be executed only if the query succeed and the resultset is not empty.
Note that in a real word component we would not directly echo field values in such a way, but more likely display them within a template.
| < Prev | Next > |
|---|






