I want to pull data from MySQL. I try this
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('total_amount')));
$query->from($db->quoteName('money'));
$db->setQuery($query);
$results = $db->loadResult();
?>
<?php echo $results; ?>
but it gives me a single result. What I want is, to pull all data from the column and put it separately. What is the mistake?
loadResult()
displays a single result from the database. What you need is loadObjectList()
.
So you query will be like so:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('total_amount')));
$query->from($db->quoteName('money'));
$db->setQuery($query);
$results = $db->loadObjectList();
// display the results
foreach ( $results as $result) {
echo "<p>" . $result->total_amount . "</p>";
}
?>
Please note that if the database table you're using is from a Joomla extension and not one you have manually created, then you should use quoteName('#__money')
. Note the #__
before the table name.
You should use loadObjectList()
instead of loadResult
and print list of objects with print_r(). More details: http://docs.joomla.org/Selecting_data_using_JDatabase
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('total_amount')));
$query->from($db->quoteName('money'));
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<?php print_r($results); ?>