phpmysqlcodeigniteractiverecordmysql-error-1064

MySQL error 1064 while using CodeIgniter's active record methods


I got the following error in codeigniter

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE appID = 'buttn_default'' at line 2

SELECT responseURL WHERE appID = 'buttn_default'

This is my model function which I'm using to get the data from the database.

function get_response_url($appID, $merchant_id)
{
    $this->db->select('responseURL');
    $this->db->from('response_urls');
    $this->db->where('appID', $appID);
    //$this->db->or_where('merchant_id', $merchant_id);
    $query = $this->db->get();
    if ($query->num_rows() == 1)
    {
        foreach($query->result() as $row)
            return $row->responseURL;
    }
    else
    {
        $appID = 'buttn_default';
        $this->db->select('responseURL');
        $this->db->where('appID',$appID);
        $query = $this->db->get();
        if ($query->num_rows() == 1)
            foreach($query->result() as $row)
                return $row->responseURL;
    }
}

Why doesn't this work? why am I getting this error?


Solution

  • Your mysql error shows that there is no from clause and your else part is also missing with from() function of active record

    else
        {
            $appID='buttn_default';
            $this->db->select('responseURL');
            $this->db->from('table_name_here');  <------
            $this->db->where('appID',$appID);
            $query = $this->db->get();
            if($query->num_rows() == 1)
            foreach($query->result() as $row)
            return $row->responseURL;
        }