13

I have a simple CodeIgniter Active record query to select an ID:

$q = $this -> db
           -> select('id')
           -> where('email', $email)
           -> limit(1)
           -> get('users');

How can I assign the above ID to a variable (eg. $id) For example,

echo "ID is" . $id;

Thanks.

10 Answers 10

20

Thats quite simple. For example, here is a random code of mine:

function news_get_by_id ( $news_id )
{

    $this->db->select('*');
    $this->db->select("DATE_FORMAT( date, '%d.%m.%Y' ) as date_human",  FALSE );
    $this->db->select("DATE_FORMAT( date, '%H:%i') as time_human",      FALSE );


    $this->db->from('news');

    $this->db->where('news_id', $news_id );


    $query = $this->db->get();

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }

}   

This will return the "row" you selected as an array so you can access it like:

$array = news_get_by_id ( 1 );
echo $array['date_human'];

I also would strongly advise, not to chain the query like you do. Always have them separately like in my code, which is clearly a lot easier to read.

Please also note that if you specify the table name in from(), you call the get() function without a parameter.

If you did not understand, feel free to ask :)

5
  • Thanks for the reply. I managed to do like: if ( $query->num_rows() > 0 ){ $row = $query->row(); return $row->id; }
    – Roman
    Jun 20, 2011 at 7:45
  • I also fine separate query much easier to read, but i found this example in codeigniter documentation. I thought it might have any advantage?
    – Roman
    Jun 20, 2011 at 7:46
  • Chained query looks "cool" but that's it. I have my entire code like this random example above. It's loooot easier to read;)
    – Herr
    Jun 20, 2011 at 7:49
  • 1
    What is the significance of FALSE as second parameter here? Feb 28, 2017 at 4:57
  • @Herr is it bad practice to use method chain? or is it related to performance? Jul 17, 2017 at 9:26
3

use Result Rows.
row() method returns a single result row.

$id = $this 
    -> db
    -> select('id')
    -> where('email', $email)
    -> limit(1)
    -> get('users')
    -> row();

then, you can simply use as you want. :)

echo "ID is" . $id;
2

one short way would be

$id = $this -> db
       -> select('id')
       -> where('email', $email)
       -> limit(1)
       -> get('users')
       -> row()
       ->id;
echo "ID is ".$id;
0
1
function news_get_by_id ( $news_id )
{

    $this->db->select('*');
    $this->db->select("DATE_FORMAT( date, '%d.%m.%Y' ) as date_human",  FALSE );
    $this->db->select("DATE_FORMAT( date, '%H:%i') as time_human",      FALSE );


    $this->db->from('news');

    $this->db->where('news_id', $news_id );


    $query = $this->db->get();

    if ( $query->num_rows() > 0 )
    {
        $row = $query->row_array();
        return $row;
    }

}   
This 
0

When use codeIgniter Framework then refer this active records link. how the data interact with structure and more.

The following functions allow you to build SQL SELECT statements.

Selecting Data

$this->db->get();

Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table:

$query = $this->db->get('mytable');

With access to each row

$query = $this->db->get('mytable');

foreach ($query->result() as $row)
{
    echo $row->title;
}

Where clues

$this->db->get_where();

EG:

 $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);

Select field

$this->db->select('title, content, date');

$query = $this->db->get('mytable');

// Produces: SELECT title, content, date FROM mytable

E.G

$this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE); 
$query = $this->db->get('mytable');
0
$query= $this->m_general->get('users' , array('id'=> $id ));
echo $query[''];

is ok ;)

1
  • 1
    Good answers will explain how to look at the obstacle the problem presents, suggest a solution and explain how the two connect. 'Do it this way because it's the best way' may be an answer, but it doesn't promote understanding. Sep 14, 2015 at 1:03
0

This is your code

$q = $this -> db
       -> select('id')
       -> where('email', $email)
       -> limit(1)
       -> get('users');

Try this

$id = $q->result()[0]->id;

or this one, it's simpler

$id = $q->row()->id;
0

echo $this->db->select('title, content, date')->get_compiled_select();

0
public function getSalary()
    {
        $this->db->select('tbl_salary.*,tbl_employee.empFirstName');
        $this->db->from('tbl_salary');
        $this->db->join('tbl_employee','tbl_employee.empID = strong texttbl_salary.salEmpID');
        $this->db->where('tbl_salary.status',0);
        $query = $this->db->get();
        return $query->result();
    }
0

Here is the example of the code:

public function getItemName()
{
    $this->db->select('Id,Name');
    $this->db->from('item');
    $this->db->where(array('Active' => 1));
    return $this->db->get()->result();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.