How to use JFactory::getDBO() in public?
I have created new php file in component. I want to get connection to Joomla database but connection unknown. Error message is the following:
Fatal error: Class 'JFactory' not found in D:\www\Joomla1.5\components\com_hello\views\hello\tmpl\index.php on line 1开发者_Go百科3
How do I use JFactory::getDBO()
in public ?
Why are you trying to get a database object in your tmpl? DB objects should only be used in either your controller or model, never in your view or tmpl. Tmpl should contain very little logic code and mostly HTML.
The idea is to get your information needed by calling a function in your model. You then pass this data by reference to your tmpl where you echo it out.
[EDIT]
$db = JFactory::getDBO();
$db->setQuery('INSERT INTO #__table (col1, col2) VALUES (val1, val2)');
$result = $db->query();
if ($result === null) {
JError::raiseWarning(100, $db->getErrorMsg());
}
Hi in my case $result === null
not worked, but !$result - worked good
$db = JFactory::getDBO();
$db->setQuery('INSERT INTO #__table (col1, col2) VALUES (val1, val2)');
$result = $db->query();
if (!$result) {
JError::raiseWarning(100, $db->getErrorMsg());
}
精彩评论