CodeIgniter Forums
Complex Sql don't work in $this->db->query() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Complex Sql don't work in $this->db->query() (/showthread.php?tid=11807)



Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

[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!


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

[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?


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

[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


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

[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.


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

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

...or try removing the new lines yourself Tongue


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

[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


Complex Sql don't work in $this->db->query() - El Forum - 09-24-2008

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