CodeIgniter Forums

Full Version: Complex Sql don't work in $this->db->query()
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]abmcr[/eluser]
I have try to use a complex query : this sql work fine into the SQL tab of phpadmin... and not into CI.

Code:
$sql="CREATE TEMPORARY TABLE tmptable
SELECT id FROM bacheca AS A ORDER BY id DESC LIMIT 30;
DELETE bacheca FROM bacheca WHERE id NOT in (SELECT * FROM tmptable)";
$this->db->query($sql);

The error is

Code:
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 ';DELETE bacheca FROM bacheca WHERE id NOT in (SELECT * FROM tmptable)' at line 1

CREATE TEMPORARY TABLE tmptable SELECT id FROM bacheca AS A ORDER BY id DESC LIMIT 30;DELETE bacheca FROM bacheca WHERE id NOT in (SELECT * FROM tmptable)

Any suggestion? Thank you in advance.... ciao!

El Forum

[eluser]richthegeek[/eluser]
I get the impression CI removes the newline characters?

Try running it with just mysql_query();

Also make sure you are running the right MySQL version?

El Forum

[eluser]mintbridge[/eluser]
Im guessing that it will be because mysql_query() doesn't support multiple queries, and the query string should not end with (or have) a semicolon

Paul

El Forum

[eluser]richthegeek[/eluser]
mysql_query DOES support multiple queries, when both the PHP and MySQL versions are on 5.x

The semicolon does matter, and is in fact meant to be there - PHP just adds it on if it's not there.

El Forum

[eluser]manilodisan[/eluser]
Try:
Code:
$this->db->query(preg_replace('/[\n\r]+/','',$sql));

...or try removing the new lines yourself Tongue

El Forum

[eluser]abmcr[/eluser]
No... i have insert all in one row, or i have try with regexp... but nothing ....it is not a CI problem, because the same sql with a normal SQL don't work.
Thank you

El Forum

[eluser]manilodisan[/eluser]
"DELETE bacheca FROM bacheca" Could this be an ambiguous naming?>