0

I was given a MySQL query:

SET @COD_PAIS = 3, @ID_CARTERA = 8; 
SELECT 
    C.DES_PERMADURACION AS Item
    , B.POR_MADURACION AS Percentage_Distribution 
FROM dficha_mes A 
JOIN det_maduraciones B 
    ON ( A.ID_FICHA_MES = B.ID_FICHA_MES ) 
JOIN mpermaduraciones C 
    ON ( B.ID_PERMADURACION = C.ID_PERMADURACION ) 
WHERE A.ID_CARTERA = @ID_CARTERA 
AND A.COD_PAIS = @COD_PAIS 
AND B.F_CORTE = ( 
                    SELECT 
                        F_ULT_CIERRE_MENSUAL 
                    FROM mpaises 
                    WHERE COD_PAIS = @COD_PAIS );

The table has rows and columns in Spanish.

When I run this query using PHP's mysql_query(), I get the following 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 'SELECT 
        C.DES_PERMADURACION AS Item
        , B.POR_MADURACION AS Percentage_Distrib' at line 2

However this query runs perfectly from MySQL Workbench or even SQLYog.

Any pointers here?

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490
Hrishikesh Choudhari
  • 11,617
  • 18
  • 61
  • 74

3 Answers3

2

You can't submit multiple queries in a single string in PHP. You'll need to do them as separate calls to mysql_query(), or use the mysqli interface instead (which is what you should be doing anyway).

http://php.net/manual/en/book.mysqli.php

siride
  • 200,666
  • 4
  • 41
  • 62
1

I believe mysql_query() can only execute 1 query at a time.

John Woo
  • 258,903
  • 69
  • 498
  • 492
1

First of all, please forget about mysql_* commands because they are deprecated.

Second, as it was stated you can not use more than one query at a time.

Third, take a look here : MySql variables and php

and here: http://www.webhostingtalk.com/showthread.php?t=360276

Community
  • 1
  • 1
Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • Thanks a lot for reminding me about this again. I'll move the codebase to PDO; its just that the legacy code had the above issue. I'll migrate soon. Thanks. – Hrishikesh Choudhari Nov 13 '12 at 05:51