Welcome Guest, Not a member yet? Register   Sign In
Selecting one value from database?
#1

[eluser]fancms[/eluser]
I need to select one value from one row in the database. With the database class I used previously before finding CI I could accomplish this by doing

Code:
$variable = $db->get_var("SELECT somevalue FROM sometable WHERE something='therowvalue'");

This would allow me to use $variable as-is throughout my code.

I've been trying to figure out how to do this (select one value from a table) in CI. Right now I have

Code:
$this->db->select('value'); #Because I need the value
$this->db->where('variable', 'siteoverview'); #Because I need the variable column entitled siteoverview
$grab_cache = $this->db->get("settings"); #From the settings table

This produces
Code:
CI_DB_mysql_result Object
(
    [conn_id] => Resource id #27
    [result_id] => Resource id #31
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] => 1
)

I'm a bit lost here. I haven't been able to find my answer in the docs, the forums, or the wiki. I know this is a basic question (and I'll likely feel pretty dumb when I find/get the answer) but I'm hoping someone will be kind enough to clarify a bit.

Thanks! Smile
#2

[eluser]ELRafael[/eluser]
Code:
$tabela = 'usuarios';
$this->db->select('nome');  //Will find 'Rafael', my name
$this->b->where('cidade', 'São Bernardo do Campo'); //Where user in my city
$query = $this->get($tabela); // Get this f&#xck; name!

$nome = $this->db->row();
$nome = $nome->nome;

echo $nome //Produces Rafael

Capisce fancms?

It's a ugly example, but i hope you understand :-P
#3

[eluser]xwero[/eluser]
[quote author="fancms" date="1193079984"]

Code:
$this->db->select('value'); #Because I need the value
$this->db->where('variable', 'siteoverview'); #Because I need the variable column entitled siteoverview
$grab_cache = $this->db->get("settings"); #From the settings table

[/quote]

You are on the right path but you have to go more specific

Code:
$this->db->select('value'); #Because I need the value
$this->db->where('variable', 'siteoverview'); #Because I need the variable column entitled siteoverview
$query = $this->db->get("settings"); #From the settings table
$row = $query->row; // get the row
echo $row->value

If you want to put it in a function

Code:
function scalar($table,$field,$where)
   $this->db->select($field); #Because I need the value
   $this->db->where($where); #Because I need the variable column    entitled siteoverview
   $query = $this->db->get($table); #From the settings table
   $row = $query->row_array(); // get the row
   return $row[$field]; // return the value
}
#4

[eluser]fancms[/eluser]
Thank you both, Rafael and xwero; it makes a lot more sense now Smile

One more question - since this is a function I'd definitely want to reuse, is it something I could make into a plugin / helper? How would I go about running the database queries from an external function file like that? When I tried saving it as a plugin, loading it in the controller file, and running the function, I got the message:

Quote:Fatal error: Using $this when not in object context
#5

[eluser]Armchair Samurai[/eluser]
If you're going to use CI resources (like the DB class) in your custom libraries, helpers and plug-ins, you need to use the get_instance() function.

Check the Creating Libraries section in the User Guide.
#6

[eluser]xwero[/eluser]
If you want to use it as a function you can extend the model library, name the file MY_Model and put it in the application/libraries directory and add following code
Code:
class MY_Model extends Model {

    function My_Model()
    {
        parent::Model();
    }

    function scalar($table,$field,$where)
    {
       $this->db->select($field);
       $this->db->where($where);
       $query = $this->db->get($table);
       $row = $query->row_array();
       return $row[$field];
    }
}
This way you don't have to load it yourself and you can use $this->scalar() to call the function.
#7

[eluser]fancms[/eluser]
Great! Thanks so much for your help! Smile




Theme © iAndrew 2016 - Forum software by © MyBB