简体   繁体   English

Joomla 3.1。 从数据库中提取数据

[英]Joomla 3.1. Pull data from database

I want to pull data from MySQL. 我想从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. loadResult()显示来自数据库的单个结果。 What you need is loadObjectList() . 您需要的是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') . 请注意,如果您使用的数据库表来自Joomla扩展,而不是您手动创建的数据库表,则应使用quoteName('#__money') Note the #__ before the table name. 注意表名称前的#__

You should use loadObjectList() instead of loadResult and print list of objects with print_r(). 您应该使用loadObjectList()而不是loadResult并使用print_r()打印对象列表。 More details: http://docs.joomla.org/Selecting_data_using_JDatabase 更多详细信息: http : //docs.joomla.org/Selecting_data_using_J数据库

<?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); ?>  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM