0

I select one table but in the error section it show me that I have selected another table.I don't know why this happens.I do query in some view, but there was no problem.This problem was not in everywhere.But in some of view and controller.

Here Was my MY_Model

protected function from()
    {
        return $this->db->from($this->_table_name);
    }

    protected function where()
    {
        if(is_array($this->_where))
        {
            foreach($this->_where as $where)
            {
                return $this->db->where($where['field_name'],$where['primary_key']);
            }
        }
        else
        {
            return $this->db->where($this->_field_name,$this->_primary_key);
        }
    }

    protected function or_where()
    {
        if(is_array($this->_where))
        {
            foreach($this->_where as $where)
            {
                return $this->db->or_where($where['field_name'],$where['primary_key']);
            }
        }
        else
        {
            $this->db->or_where($this->_or_field_name,$this->_or_primary_key);
        }
    }

    protected function join()
    {
        if(is_array($this->_join))
        {
            foreach($this->_join as $join)
            {
                return $this->db->join($join['table'],$join['query']);
            }
        }
        else
        {
            return 'Invalid Query';
        }
    }

    protected function order()
    {
        //print_r($this->_order);exit();
        if(is_array($this->_order))
        {
            return $this->db->order_by($this->_order['by'],$this->_order['order']);
        }
        else
        {
            return "Invalid Query";
        }
    }

    protected function limit()
    {
        if(is_array($this->_limit))
        {
            foreach($this->_limit as $limit)
            {
                return $this->db->limit($limit['from'],$limit['to']);
            }
        }
        else
        {
            return $this->db->limit($this->_limit);
        }
    }

    public function get()
    {
        $this->db->select('*');
        if($this->_table_name)
        {
            $this->from();
        }
        if($this->_where || ($this->_field_name && $this->_primary_key))
        {
            $this->where();
        }
        if($this->_or_where || ($this->_or_field_name && $this->_or_primary_key))
        {
            $this->or_where();
        }
        if($this->_join)
        {
            $this->join();
        }
        if($this->_order)
        {
            $this->order();
        }
        if($this->_limit)
        {
            $this->limit();
        }

        return $this->db->get();
    }

And this is my query

    $this->cm->_table_name = "products";
            $this->cm->_field_name = "products.productID";
            $this->cm->_primary_key = $productID;
            $this->cm->_join = array
            (
                array
                (
                    'table' => 'product_details',
                    'query' => 'products.productID=product_details.productID',
                    'type' => 'left',
                ),
            );
            $data['product'] = $this->cm->get();

this is the error

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM 'products', 'parentcategory' WHERE 'products'.'productID' = '30'' at line 1

SELECT *, * FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

Filename: C:/xampp/htdocs/mv/system/database/DB_driver.php

Line Number: 691

I don't know why this happens please help me.

1 Answer 1

1

change your query with below:

SELECT products.*, parentcategory.* FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

or

SELECT * FROM `products`, `parentcategory` WHERE `products`.`productID` = '30'

You can add another parameter like:

$this->cm->_field_to_select = "products.*,parentcategory.*";

and add it in your get method

if ($this->_field_to_select) {
    $this->db->select($this->_field_to_select);     
}
else {  
    $this->db->select('*');
}
Sign up to request clarification or add additional context in comments.

9 Comments

why this happens ?
i dont put parent category is this query, but this comes automatically .
as @AbdullaNilam said , because of $this->db->select('*'); either remove it or provide parameter to set select fields
so i need to remove $this->db->select('*') from MY_Model or from query ?
if it is specifically targeting only that query then remove it otherwise add another param to set select fields
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.