php CodeIgniter 选择查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6408059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:17:23  来源:igfitidea点击:

CodeIgniter Select Query

phpmysqlcodeignitervariablesselect

提问by Roman

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

我有一个简单的 CodeIgniter 活动记录查询来选择一个 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,

如何将上述 ID 分配给变量(例如 $id) 例如,

echo "ID is" . $id;

Thanks.

谢谢。

回答by Herr

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 chainthe 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.

另请注意,如果在 from() 中指定表名,则调用 get() 函数时不带参数

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

如果你不明白,请随时问:)

回答by Raza Ahmed

one short way would be

一种简短的方法是

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

回答by Pasindu Jayanath

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

使用结果行
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;

回答by Pasindu Jayanath

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 

回答by Steve Sharax

$query= $this->m_general->get('users' , array('id'=> $id ));
echo $query[''];

is ok ;)

没关系;)

回答by Vindicated

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;

回答by Arnold Machado

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

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

回答by Saravanan

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();
    }

回答by Supriya

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();
}

回答by Ravi Chauhan

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

当使用codeIgniterFramework 时,请参考此活动记录链接。数据如何与结构交互等等。

The following functions allow you to build SQL SELECT statements.

以下函数允许您构建 SQL SELECT 语句。

Selecting Data

选择数据

$this->db->get();

$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');