5

我是 joomla 的新手,我正在尝试构建一个组件,它是 viruemart 的插件,以允许用户在其用户区域访问发票 PDF。为此,我需要编写一个选择查询,为我从数据库中检索此信息。

我一直在搞乱它并想出了以下内容,但它似乎没有做任何事情:

$id =JFactory::getUser()->id;
$db =& JFactory::getDBO();
$sql = "SELECT * FROM jos_vm_orders"; 
$db->setQuery($sql);
$options = $db->loadObjectList();
return $options; 

我错过了什么吗?

4

4 回答 4

9

您可以查看此文档以了解数据库查询 - http://docs.joomla.org/API16:JDatabaseQuery

交叉检查您的表前缀。或者试试这个 -

$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__vm_orders'); 
$query->where('id = 1');   //put your condition here    
$db->setQuery($query);
//echo $db->getQuery();exit;//SQL query string  
//check if error
if ($db->getErrorNum()) {
  echo $db->getErrorMsg();
  exit;
}
return $db->loadObjectList();
于 2012-11-01T12:08:42.557 回答
2
$db =& JFactory::getDBO();       
$query  = $db->getQuery(true);
$query->select('*');
$query->from('#__vm_orders');      
$db->setQuery($query);  
$options = $db->loadObjectList();
return $options;

或者

$db =& JFactory::getDBO();
$sql = "SELECT * FROM #__vm_orders";
$db->setQuery($sql);  
$options = $db->loadObjectList();
return $options;

试试这个,如果您有任何问题,请告诉我。

于 2012-11-01T12:14:58.320 回答
1

试试这个

$user =JFactory::getUser();
$userId = $user->id;//also u get name,email etc
$db =& JFactory::getDBO();
$sql = "SELECT * FROM table where condition"; 
$db->setQuery($sql);
$db->query();
$options = $db->loadObjectList();
return $options; 
于 2012-11-01T12:35:20.457 回答
1
$id =JFactory::getUser()->id;
$db =& JFactory::getDBO();
$sql = "SELECT * FROM #__vm_orders"; 
$db->setQuery($sql);
$options = $db->loadObjectList();
return $options;

I would also recommend to use query chaining when using complex queries for Joomla 2.5 and futher version. Here you read about it : http://m4extensions.com/index.php/tutorials/3-access-database-from-your-joomla-extension

于 2014-02-23T11:13:55.920 回答