Doenitz Galán
Doenitz Galán

Reputation: 21

SQL sub query from PHP PDO query

i have a question, i'm trying to execute a MSSQL query from PHP, i executed the query directly in SQL SERVER and not problem, but if i execute the query from PHP PDO i don't get any result, testing the query without the sub query i got results without problems but if i do it with the sub query i don't get any result

The complete query is:

SELECT A.MATNR, B.MAKTX, C.EAN11, C.MEINH, C.HPEAN FROM FJBK.DBO.MARA AS A INNER JOIN FJBK.DBO.MAKT AS B ON A.MATNR = B.MATNR INNER JOIN FJBK.DBO.MEAN AS C ON A.MATNR = C.MATNR WHERE c.matnr = (select matnr from MEAN where ean11 = '.$articulo.') 

if i execute the query in this way:

SELECT A.MATNR, B.MAKTX, C.EAN11, C.MEINH, C.HPEAN FROM FJBK.DBO.MARA AS A INNER JOIN FJBK.DBO.MAKT AS B ON A.MATNR = B.MATNR INNER JOIN FJBK.DBO.MEAN AS C ON A.MATNR = C.MATNR WHERE c.matnr = 1000256 

i'm getting results.

i test the query a lot of times thinking that could be the PHP var in the query but isn't the problem

From PHP the sub query doesn´t return results, but directly in the MSSQL work without problems, i use this complete query in a Visual Basic.NET app and is working fine.

The problem is execute the subquery in the principal query from PHP

Upvotes: 1

Views: 503

Answers (1)

atoms
atoms

Reputation: 3093

Try changing:

where ean11 = '.$articulo.'

to

where ean11 = '\'.$articulo.'\')

or use double quotes to contain your sql and use

$sSQL = "SELECT A.MATNR, B.MAKTX, C.EAN11, C.MEINH, C.HPEAN
         FROM FJBK.DBO.MARA AS A
           INNER JOIN FJBK.DBO.MAKT AS B
             ON A.MATNR = B.MATNR
           INNER JOIN FJBK.DBO.MEAN AS C
             ON A.MATNR = C.MATNR
         WHERE c.matnr = (
           SELECT matnr 
           FROM MEAN 
           WHERE ean11 = '".$articulo."'
         )";

Upvotes: 0

Related Questions