Selecting one value from database? |
[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 produces Code: CI_DB_mysql_result Object 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! ![]()
[eluser]ELRafael[/eluser]
Code: $tabela = 'usuarios'; Capisce fancms? It's a ugly example, but i hope you understand :-P
[eluser]xwero[/eluser]
[quote author="fancms" date="1193079984"] Code: $this->db->select('value'); #Because I need the value [/quote] You are on the right path but you have to go more specific Code: $this->db->select('value'); #Because I need the value If you want to put it in a function Code: function scalar($table,$field,$where)
[eluser]fancms[/eluser]
Thank you both, Rafael and xwero; it makes a lot more sense now ![]() 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
[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.
[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 {
|
Welcome Guest, Not a member yet? Register Sign In |