php 如何在 joomla 2.5 中构建 sql 选择查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13176616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to build a sql select query in joomla 2.5?
提问by Sean Lang
im new to joomla and im trying to build a component which is an addon for viruemart to allow users to access invoice PDF's in their user area. To do this i need to write a select query which retreives this information from the database for me.
我是 joomla 的新手,我正在尝试构建一个组件,该组件是 viruemart 的插件,允许用户访问其用户区域中的发票 PDF。为此,我需要编写一个选择查询,为我从数据库中检索此信息。
I have been messing around with it and came up with the following, but it doesnt seem to do anything:
我一直在搞乱它并想出了以下内容,但它似乎没有做任何事情:
$id =JFactory::getUser()->id;
$db =& JFactory::getDBO();
$sql = "SELECT * FROM jos_vm_orders";
$db->setQuery($sql);
$options = $db->loadObjectList();
return $options;
Am i missing something?
我错过了什么吗?
回答by Irfan
You can check this doc for database queries - http://docs.joomla.org/API16:JDatabaseQuery
您可以查看此文档以获取数据库查询 - http://docs.joomla.org/API16:JDatabaseQuery
Cross check your table prefix.Or try this-
交叉检查你的表前缀。或者试试这个-
$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();
回答by Techie
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__vm_orders');
$db->setQuery($query);
$options = $db->loadObjectList();
return $options;
OR
或者
$db =& JFactory::getDBO();
$sql = "SELECT * FROM #__vm_orders";
$db->setQuery($sql);
$options = $db->loadObjectList();
return $options;
Try this and let me know if you have any issues.
试试这个,如果你有任何问题,请告诉我。
回答by Jobin Jose
Try This
尝试这个
$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;
回答by Marrouchi
$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
我还建议在对 Joomla 2.5 和更高版本使用复杂查询时使用查询链。在这里你读到它:http: //m4extensions.com/index.php/tutorials/3-access-database-from-your-joomla-extension

