可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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 :)
回答2:
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;
回答3:
one short way would be
$id = $this -> db
-> select('id')
-> where('email', $email)
-> limit(1)
-> get('users')
-> row()
->id;
echo "ID is ".$id;
回答4:
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
回答5:
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');
回答6:
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;
回答7:
echo $this->db->select('title, content, date')->get_compiled_select();
回答8:
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();
}
回答9:
$query= $this->m_general->get('users' , array('id'=> $id ));
echo $query[''];
is ok ;)