Rizwan Khan
Rizwan Khan

Reputation: 401

Please tell me what's wrong in my sql query?

i am creating an application in codeigniter. I am creating a function of searching tutorial from database. But i am getting error. Please check my code.

    function get_tuts($query, $limit, $offset) {

    $offset = 0;
    $sql = "SELECT * FROM tutorials WHERE  MATCH ( title, desc ) AGAINST (?) LIMIT ? OFFSET ?"; 
    $q = $this->db->query($sql, array($query, $limit, $offset));

    if($q->num_rows() > 0){
                foreach($row->result() as $row){
                    $data[] = $row;
                }
            }
            return $data;
}

but i am getting this error

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 'desc ) AGAINST ('yahoo') LIMIT 10 OFFSET 0' at line 1

Upvotes: 1

Views: 126

Answers (3)

SlavaNov
SlavaNov

Reputation: 2485

desc is a reserved word. Add ` to desc:

$sql = "SELECT * FROM tutorials WHERE  MATCH ( title, `desc` ) AGAINST (?) LIMIT ? OFFSET ?"; 

Upvotes: 2

Lion
Lion

Reputation: 19027

The error message you're receiving says that desc is a reserved keyword in MySql.

use near 'desc ) AGAINST ('yahoo') LIMIT 10 OFFSET 0' at line 1

Upvotes: 1

Wes Crow
Wes Crow

Reputation: 2967

The word desc is a reserved word in mysql. Try surrounding the column name with backticks `.

Upvotes: 3

Related Questions