1

I am running the following query :

SELECT @newNo := MAX( category_code ) FROM category_master;
INSERT INTO category_master VALUES (@newNo +1,  'Test')

The query runs flawlessly in phpmyadmin but it shows a database error when run using codeigniter :

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 'INSERT INTO category_master VALUES(@newNo+1, 'Test')' at line 2

what could be the reason ?? In codeigniter model i use the following code :

$query = 'SELECT @newNo := MAX(category_code) FROM category_master;
                    INSERT INTO category_master VALUES(@newNo+1, 
                    \''.$category_name.'\')';
$result = $this -> db -> query($query);

2 Answers 2

4

You cannot run two queries at once. Seperate them:

$query = 'SELECT @newNo := MAX(category_code) FROM category_master';
$result = $this->db->query($query);

$query = 'INSERT INTO category_master VALUES(@newNo+1, \''.$category_name.'\')';
$result = $this->db->query($query);

EDIT: On your second query it is recommended to use query bindings:

$query = 'INSERT INTO category_master VALUES(@newNo+1, ?)';
$result = $this->db->query($query, $category_name);
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure the query does not contains any special characters. The browser will convert the special characters, so the query is running in phpmyadmin. To know the special characters, echo the query and copy it and paste in an editor like dreamweaver, it will show you the special characters. Hope this helps.

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.