【问题标题】:CodeIgniter Select QueryCodeIgniter 选择查询
【发布时间】:2011-09-18 11:31:49
【问题描述】:

我有一个简单的 CodeIgniter 活动记录查询来选择一个 ID:

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

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

echo "ID is" . $id;

谢谢。

【问题讨论】:

    标签: php mysql codeigniter variables select


    【解决方案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;
        }
    
    }   
    

    这将返回您选择的“行”作为数组,以便您可以像这样访问它:

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

    我也强烈建议,不要像你一样链接查询。总是像在我的代码中一样将它们分开,这显然更容易阅读。

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

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

    【讨论】:

    • 感谢您的回复。我设法做到了:if ( $query->num_rows() > 0 ){ $row = $query->row(); return $row->id; }
    • 我也很好,单独的查询更容易阅读,但我在 codeigniter 文档中找到了这个例子。我认为它可能有什么优势?
    • 链式查询看起来很“酷”,但仅此而已。我有我的整个代码,就像上面这个随机示例一样。它更容易阅读;)
    • 这里第二个参数FALSE有什么意义?
    • @Herr 使用方法链是不好的做法吗?还是与性能有关?
    【解决方案2】:

    使用 Result Rows
    row() 方法返回单个结果行。

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

    然后,您可以随意使用。 :)

    echo "ID is" . $id;
    

    【讨论】:

      【解决方案3】:

      一个捷径是

      $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】:

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

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

          选择数据

          $this->db->get();

          运行选择查询并返回结果。可单独用于检索表中的所有记录:

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

          可以访问每一行

          $query = $this->db->get('mytable');
          
          foreach ($query->result() as $row)
          {
              echo $row->title;
          }
          

          哪里有线索

          $this->db->get_where();
          

          EG:

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

          选择字段

          $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】:
            $query= $this->m_general->get('users' , array('id'=> $id ));
            echo $query[''];
            

            没问题;)

            【讨论】:

            • 好的答案将解释如何看待问题提出的障碍,提出解决方案并解释两者如何联系。 “这样做是因为它是最好的方式”可能是一个答案,但它不会促进理解
            【解决方案7】:

            这是你的代码

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

            试试这个

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

            还是这个,更简单

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

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:
                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();
                    }
                

                【讨论】:

                  【解决方案10】:

                  下面是代码示例:

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

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-05-08
                    • 2016-07-19
                    • 2017-12-28
                    • 2015-02-02
                    • 2023-03-17
                    • 2014-08-26
                    相关资源
                    最近更新 更多