Custom Mysql(I) Query In Cs-Cart

Helllo there,
I am working on CPanel at orders section of cs-cart.. I would like to grab the usergroup_id of the current customer I am watching the order he/she maid.
So, I have this line of code inside tpl file:
{include file="common/usergroup.tpl" id=$order_info.user_id}
usergroup.tpl is a custom file that I have create. Now, in this file I would like to create a mysql_query in order to take the usergroup of this user ($order_info.user_id) for example 54.. I have this query:
$_usergroup = db_query('SELECT usergroup_id FROM cscart_usergroup_links WHERE user_id=$id');
{if $_usergroup == 3}
hello!
{/if}
How can I do this query to work?
thanks in advance!

Think you're overcomplicating. You probably want logic more like:

if( !empty($_SESSION['auth']['user_id']) &&  !empty($_SESSION['auth']['usergroup_ids']) ) {
  // do whatever you want with the array of usergroup_id's of the user
} else {
  // User is not logged in or no usergroups defined so do what you want in this case
}

If you're wanting to do this post-facto in the admin for any arbitrary order, then you would

if( !empty($order_info['usergroups']) ) {
  $usergroup_ids = array_keys($order_info['usergroups']);
  if( $usergroup_ids ) {
    // do what you want with the array of usergroup_ids
  }
}

Note all the above is intended to show logic only and may not be suitable for your use. And if you want it in *.tpl files, then you'd have to use the same logic in smarty code.

Not necessary in .tpl files. Let me explain a bit more. I am looking the orders that I have in my e-shop through the cpanel of cs-cart, right? Let's say I have 10 orders from different persons... Furthermore, I have two kinds of customers! I would like to know in which customer category belongs each of these 10 orders. If I click one of these orders to see more details, through the debug I can see every available information... order_info, auth an so on.. Inside the array of auth the usergroup_ids is always zero( array(0) ... and nothing more). The session array is referring on my login, not the customer I am looking at his/her order, right? So, inside the code of orders.tpl I can see the id of this user and wants to know about the usergroup_ids
I do not know... Maybe I am missing something? on the other hand, I will try what you said and come back to this post..

Thank you for your time!

I believe that if the user was assigned to usergroups (not the standard Registered, Guest groups) that there should be a 'usergroup' array in the user_data area of the order. It is possible that this is only while the order is in the 'cart' versus an 'order' since there wouldn't really be any need for usergroup info after the order is place other than for auditing.

So you can use logic of:

if( $order_info['user_id'] ) {
    $usergroups = db_get_hash_array("SELECT ul.usergroup_id, ud.usergroup FROM ?:usergroup_links AS ul
                                    LEFT JOIN ?:usergroup_descriptions AS ud ON ul.usergroup_id=ud.usergroup_id AND lang_code=?s
                                    WHERE user_id=?i", 'usergroup_id', DESCR_SL, $order_info['user_id']);
    // $usergroups now contains like:
    //        array(3 => array('usergroup_id'=>3, 'usergroup'=>'Wholesale'),
    //                4 => array('usergroup_id'=>4, 'usergroup'=>"Some other group"));
}
And if you want to put this in template code, make the above a function like 'my_usergroups($order_info['user_id'])' and then use it as a variable modifier like:
{$my_usergroups = $order_info.user_id|my_usergroups}
{foreach from=$my_usergroups key="usergroup_id" item="v"}
  usergroup_id={$usergroup_id}, name={$v.usergroup}
{/foreach}

Please try

{$_usergroups = "SELECT usergroup_id FROM ?:usergroup_links WHERE user_id=?i AND status=?s"|db_get_fields:$id:'A'}

{if in_array(3, $_usergroups)}
hello!
{/if}

Thank you all for your answers and your help. I appreciated that! I think eComLabs is exactly the answer I am looking for! I will give it a try, when I go back to office and let you know the results. thank you for your time