Joomla Database Query With Objects

Joomla Database Query in Module or Extension

The code belows how objects can be fetched from a database with Joomla and pure php / mysql. The main differences in syntax are the database connection variable and the naming of the database table. The two examples below clearly show the differences.

JOOMLA

$db =& JFactory::getDBO();
$query = "SELECT name from #__mytable WHERE enabled=1 ";
$db->setQuery($query);

$rows = $db->loadObjectList();
foreach ($rows as $row) {
echo "<b>".$row->name."</b> records which are enabled.<br/><hr>";
}

PHP / MYSQL
$query = "SELECT name from mytable WHERE enabled=1 ";
$result = mysqli_query($db, $query);
while ($row=mysqli_fetch_object($result)) {
$myname=$row->name;
echo $myname;
}