Joomla Module Database Query

Joomla Database Query in Module or Extension

The code belows how arrays can be fetched from a database with Joomla and pure php / mysql. The main differences in syntax are the databse 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->loadAssocList();
//print_r($rows);
foreach ($rows as $row) {
echo "<br/><br/>My loadAssocList() method for the associative array loop count is <b>" .$row['name']."</b> records which are enabled.<br/>";
}

PHP / MYSQL
$query = "SELECT name from mytable WHERE enabled=1 ";
$result = mysqli_query($db, $query);
while ($row=mysqli_fetch_assoc($result)) {
$myname=$row['name'];
echo $myname;
}