CodeIgniter Select Query

前端 未结 10 973
刺人心
刺人心 2020-12-10 04:37

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

$q = $this -> db
           -> select(\'id\')
           -> where(\'email\', $email         


        
相关标签:
10条回答
  • 2020-12-10 05:15

    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;
    
    0 讨论(0)
  • 2020-12-10 05:16
    $query= $this->m_general->get('users' , array('id'=> $id ));
    echo $query[''];
    

    is ok ;)

    0 讨论(0)
  • 2020-12-10 05:17

    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 讨论(0)
  • 2020-12-10 05:18

    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();
    }
    
    0 讨论(0)
  • 2020-12-10 05:21

    one short way would be

    $id = $this -> db
           -> select('id')
           -> where('email', $email)
           -> limit(1)
           -> get('users')
           -> row()
           ->id;
    echo "ID is ".$id;
    
    0 讨论(0)
  • 2020-12-10 05:24

    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 :)

    0 讨论(0)
提交回复
热议问题