0

Below is my model code, in which I'm getting a database error:

public function getxyz()
{
    $query ='SELECT DISTINCT(LEFT(field1,LOCATE('-',field1) - 1)) as field1
    FROM table1';
    $res = $this->db->query($query);
    return $res->result();
}

Note: This query is running successfully in phpmyadmin. Any help will be appreciated. Thanks in advance.

0

2 Answers 2

3

I think you have an error in the query related to the single quotes you are using to enclose the query. They are interfering with the single quotes to the string - seems out of the query. Try this query:

 $query ="SELECT DISTINCT(LEFT(field1,LOCATE('-',field1) - 1)) as field1
        FROM table1";
Sign up to request clarification or add additional context in comments.

Comments

0

Your query is working fine PHPMYADMIIN because there is no issue with quotes around SQL statement.

You just need to fix single and double quote issue in your query.

Example:

public function getxyz()
{
    $query ="SELECT DISTINCT(LEFT(field1,LOCATE('-',field1) - 1)) as field1
    FROM table1";
    $res = $this->db->query($query);
    return $res->result();
}

Comments

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.