Joomla! JDatabase::getErrorNum() is deprecated, use exception handling instead - joomla

I have the following code:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__users');
$db->setQuery($query);
// Check for a database error.
if ($db->getErrorNum()) {
JError::raiseWarning(500, $db->getErrorMsg());
}
$result = $db->loadResult();
Now getErrorNum as well as JError are deprecated.
Just to clarify, JError and $db->getErrorNum() are NOT deprecated in Joomla 2.5 but are in Joomla! 3.0. So this question has value for somebody who develops for 2.5 but wants to make an easy upgrade to 3.X series.
So with what exactly to replace them so that I can properly check for database errors?

$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__users');
try
{
$db->setQuery($query);
$result = $db->loadResult();
}
catch (RuntimeException $e)
{
echo $e->getMessage();
}
Hope it helps

JError is NOT deprecated in Joomla 2.5 - but in Joomla 3.0 (Platform 12.1 onwards) so for Joomla 2.5 this is not an issue. JError is being replaced with the php exceptions (link to the php guide here)
There is also a Joomla forum question about this here
Using exception is also the case for getErrorNum() - again read the php documentation link above for more info. There's a nice mysql example on the page here.

$app = JFactory::getApplication();
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*')
->from('#__tablename');
try
{
$db->setQuery($query);
$result = $db->loadObjectList();
}
catch (Exception $e)
{
$app->enqueueMessage(JText::_($e->getMessage()), 'error');
}
and redirect to your url. Error message will be displayed

Related

PHP code not working on ipage hosting?

My code is working well on localhost and also on godaddy server.But on ipage I am getting "500 Internal Server error" with this code.
My local server has PHP Version 5.4.7 and my ipage hosting has PHP Version 5.3.13
public function cms($cid,$vars) {
global $db;
//fetch cms data from database
$stmt = $db->prepare("SELECT * FROM cms");
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
if ($cid == ""){
$stmt = $db->prepare("SELECT * FROM cms");
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
else {
$stmt = $db->prepare("SELECT * FROM cms WHERE id=? ");
$stmt->execute(array($cid));
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
if ($vars == "") {
return $row;
}
else {
$ret = [];
foreach ($vars as $var) {
$ret[] = [
$var => $row[0][$var]
];
}
return $ret;
}
}
}
I think its version problem and for that you need to ask your hosting provider(ipage) to make your php version same as your local version..
Also check out your .htaccess file too, cos. some time redirection can also give this types of error.
if anyone facing internal server (500) error in ipage, you can chat or call them, then they will upgrade your version to 7 or above.
you can check your php version using this code.
<?php
phpinfo();
?>

Call function inside Joomla articles

I am facing a problem. I have created a article in Joomla administrator and i have created a function in a php file. I have to call that function in that article . I don't know how do i call function in article.
Here is the code:
class modVodesbalanceHelper {
function deductBalance()
{
$db = JFactory::getDBO();
$result = null;
$user = JFactory::getUser();
if ($user->guest) {
return false;
}
$query = 'SELECT credit' .
' FROM #__vodes_credits' .
' WHERE userid = ' . (int) $user->id
;
$db->setQuery($query);
$result = $db->loadResult();
$result_final=$result-10;
$query = 'update #__vodes_credits SET credit='.$result_final.
' WHERE userid = ' . (int) $user->id
;
//echo $query;
$db->setQuery($query);
$result = $db->loadResult();
}
}
In admin panel, in article i have write this code:
<script>
window.onload=function()
{
var a=confirm("do you want to purchase this credit");
if(a)
{
document.location.href ="index.php?option=com_content&view=featured";
}
else
{
document.location.href="index.php?option=com_content&view=article&id=3";
}
}
</script>
I have to call deductBalance when user click on the "OK" of comfirm box.
Please tell me to sought it .
You can't add PHP to Joomla article by default. For this you need to use a 3rd party plugin. Personally, I would recommend using Sourcerer. Once installed and enabled, you can use the following in your article:
{source}
<?php
// All your PHP code
?>
{/source}
Don't include PHP files inside your article
I would use Sourcerer to add php code inside joomla article.
Check the sourcerer plugin to be enabled (check plugin enabled status here: extensions->plugin manager->sourcerer).
Then, use the "Insert Code" button at the bottom of your WYSIWYG editor when adding your PHP scripts. Your code should be wrapped with
{source}
//php code wrapped by <?php ?>
{/source}
If you need to use code directly within Joomla article then use Sourcerer as in above questions. If you feel to keep the PHP code in seperate module. Use Blank Module
Or mod_php module. Then in your Joomla Article you can place the code like this,
{loadposition}*Your Custom Module Position*{/loadposition}

Displaying list of logged in users in Front side using Joomla 2.5 component

I would like to display all login user list in Front side using Joomla 2.5 component.
Can any body tell me how do do this?
I would also want to develop change password and news subscription module.
Try this,
$db =JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__users');
$db->setQuery($query);
$rows = $db->loadObjectList();
foreach ($rows as $row) {
$user = JFactory::getUser($row->id);
$status = $user->guest;
if(!$status){
echo $row->name.'---Email'.$row->email;
}
}
Use this extension to implement its in your site if you need quick solution.
Hope it helps..

Getting title of a category that has id 80

$db = JFactory::getDBO();
$db->setQuery('SELECT title FROM #__categories WHERE id = 80');
$category = $db->loadResult();
echo $category;
Can anyone tell me why this does not return the title of category with id 80?
and/or is there a better way of doing this? I have an item that shows the id but not the name/title
Try using the following which uses Joomla 2.5 coding standards:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('title')
->from('#__categories')
->where('id = 80');
$db->setQuery($query);
$result = $db->loadResult();
echo $result;
As nibra mentioned, you can also check to see if it exists like so:
if($result){
echo $result;
}
else {
echo "title with this ID was not found";
}
Your code works, if there is a category with the id 80. So
if $category is null, there is no such category;
if $category is something else, that is the title of your category.
Beide that, the better way to access the database in Joomla! is as Lodder pointed out.

Joomla 2.5 database $query->where warning

I have this simple code to select custom string from database:
protected function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__person');
$name = 'tom';
$query->where('name LIKE %'.$db->quote($name).'%');
return $query;
}
Unfortunately it gives me an error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean
given in xxx\public\libraries\joomla\database\database\mysql.php on
line 293
If I remove where call, so everything goes ok. Can I debug the datase query? I would like to see whats the final query goes to MySQL server.
Your help would be appreciated.
I've managed to work this for me:
protected function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
->from('#__person');
$name = 'tom';
$name = $db->Quote('%'.$db->escape($name, true).'%');
$query->where($db->nameQuote('name').' LIKE '.$name);
//debug the query
// echo nl2br(str_replace('#__','prefix_',$query)); die;
return $query;
}

Resources