Part of the EllisLab Network
   
 
Selecting one value from database?
Posted: 22 October 2007 09:06 AM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  35
Joined  10-20-2007

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

$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

$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

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

 Signature 

PHP + CodeIgniter LOL

Profile
 
 
Posted: 22 October 2007 09:18 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  298
Joined  09-11-2007
$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 tongue laugh

 Signature 

cool mad
Rafael
Last.fm
Twitter

“Because they’re stupid, that’s why. That’s why everybody does everything!”

Profile
 
 
Posted: 22 October 2007 09:25 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006
fancms - 22 October 2007 09:06 AM
$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

 

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

$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

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
}
Profile
 
 
Posted: 22 October 2007 09:46 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  35
Joined  10-20-2007

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:

Fatal error: Using $this when not in object context

 Signature 

PHP + CodeIgniter LOL

Profile
 
 
Posted: 22 October 2007 10:15 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
RankRank
Total Posts:  237
Joined  10-02-2007

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.

 Signature 

ウェブデザインイラストレーショングラフィックデザイン
“If I have to be smarter to use your technology, then your technology sucks.”

Profile
 
 
Posted: 22 October 2007 10:27 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

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

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.

Profile
 
 
Posted: 22 October 2007 11:33 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  35
Joined  10-20-2007

Great! Thanks so much for your help! smile

 Signature 

PHP + CodeIgniter LOL

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120532 Total Logged-in Users: 37
Total Topics: 126594 Total Anonymous Users: 3
Total Replies: 665537 Total Guests: 378
Total Posts: 792131    
Members ( View Memberlist )
Newest Members:  Rach123Glenn StavaImfaelSMS!persanulandrewknightKittx77AmityBetbulJahhn