Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
DB Class question
  • In CodeIgniter, I often set up a database query similar to below where if an id is passed to the function, only the info for the row associated with that id is returned, otherwise all rows are returned. Note the "if" clause. Is there a way to do something similar using Fuel CI code:
    function get_client_info($client_id = null
    {
    {
    $this->db->select('*');
    $this->db->from('clients'); if ($client_id != null)
    $this->db->where('client_id', $client_id, TRUE); $query = $this->db->get();
    $data = $query->result_array();
    // Etc.
    } Basic Fuel Code:
    function get_client_info($client_id = null
    {
    $result = DB::select('*')
    ->from("clients")
    ->execute()
    ;
  • Next time use [ code] and [/ code] (without the spaces ofcourse. Also I think you got CI & Fuel mixed up in your examples.
    function get_client_info($client_id = null)
    {
        $query = DB::select('*')->from('clients');
    
        if ($client_id != null)
        {
            $query->where('client_id', '=', $client_id);
        }
    
        $data = $query->execute()->as_array();
        // Etc.
    }
    
  • Thanks Jelmer. That worked perfectly and now it's clearer how DB::query works.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion