Sean Lang
Sean Lang

Reputation: 422

how to build a sql select query in joomla 2.5?

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.

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?

Upvotes: 5

Views: 29427

Answers (4)

Marrouchi
Marrouchi

Reputation: 281

$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

Upvotes: 1

Irfan
Irfan

Reputation: 7059

You can check this doc for database queries - 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();

Upvotes: 10

Jobin
Jobin

Reputation: 8282

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; 

Upvotes: 1

Techie
Techie

Reputation: 45124

$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.

Upvotes: 2

Related Questions