Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a query's result exists in Joomla php function?

I've created a function in Joomla. It checks if the given user's id exists in the database's table with an 'IF' statement. I think there's something wrong with the condition I'm using for the 'IF' statement.

Here's the code:

    public function setRemainingPoints($userId,$points_taken) 
    {
    $db     =& JFactory::getDBO();

    $query  =   ' SELECT '
               .$db->nameQuote('remaining_points').' FROM '
               .$db->nameQuote('#__remainingpoints').' WHERE '
               .$db->nameQuote('user_id').'='.$userId;  

    $db->setQuery( $query );

    if ($result = $db->query()) 
    {               
    /* code if user found */
    }
    else
    {
    /* code if user not found */
    }   
    return true;
    }

Thanks in advance.

like image 399
Sarah Avatar asked Mar 08 '26 01:03

Sarah


1 Answers

You need to get its number rows and make condition in this way

$db->setQuery($query);
$db->query();
$num_rows = $db->getNumRows();
if($num_rows>0){
// do something
}
else{
// do another things
}
like image 171
Shivomkara Chaturvedi Avatar answered Mar 09 '26 14:03

Shivomkara Chaturvedi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!