CodeIgniter Forums

Full Version: Assign value from SQL to variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys!

I have a very simple question.

I would like to assign SQL-query result (which is only 1 row and 1 column) to my variable, and when make calculations with them.

This is SQL select query, which should be assigned to variable $timezone:

Code:
'SELECT gtm FROM wh_rates WHERE (city_id = "'.$sity_id.'" OR city_id_en = "'.$sity_id.'") LIMIT 1'

This is why I need variable $timezone (to make it clear for you):


Code:
$current_day = gmdate("N", time() + 3600*($timezone+date("I")));
$current_time = gmdate("H", time() + 3600*($timezone+date("I")));



Can anybody help me?   Cool
@dmitrykanaev,
1st question...Have you tested the query in the database to make sure that it works?  
2nd question...What is the default value if for some reason your select query returns null?
More questions...Are you returning just the value from the model to the variable $timezone?  What is the name of the model that should be returning the variable? What is an example gtm value that you are expecting?

My suggested solution is as follows...

CONTROLLER (statement in controller):


PHP Code:
//  call model method to get timezone value from database based on  $sity_id
$timezone $this->Model_timezone->get_timezone($sity_id); 




MODEL (Model_timezone with a get_timezone method):

PHP Code:
/**
*
*  1234 - is an example default value.
*  default_value - some default value in the event that the query returns a null value
*
**/
public function get_timezone$sity_id=1234)
{
...
$sql 'SELECT gtm FROM wh_rates WHERE (city_id = ? OR city_id_en = ? ) LIMIT 1';

$result $this->db->query($sql, array($sity_id,$sity_id));

// return value from query or default value if return value is null
return (is_null($result['gtm']))? default_value $result['gtm'];