How to connect the Db and get the data from table in Joomla php
I want to connect the Joomla DB and i have created table in PHPmyAdmin. And want to get the row and val开发者_如何学Pythonues from the field.
Best way when having a proper JTable:
$row =& JTable::getInstance('my_table', '');
$row->load($id);
When working without JTable:
$db = &JFactory::getDbo();
$sql = 'SELECT * FROM #__table WHERE id ='.$id;
$db->setQuery($sql);
$object = $db->loadObject();
OFC you can make that more elegant inside MVC. BUT for starters maybe enough.
$db = JFactory::getDBO();
$query = "select id,title from #__content";
$db->setQuery($query);
$result = $db->loadObjectList();//here we got array of stdClass===prepared objects in PHP 5.3
foreach($result as $res){
echo $res->id."".$res->title;
}
精彩评论