1

I'm getting this error after passing a variable containing the query inside the FROM(). The query works on workbench but not on php.

This is the error:

["Error in Selecting ","Error in Selecting 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 ')cnt' at line 1 SELECT SUM(counting) FROM()cnt"]

And the variable that generates the error:

$summ = "SELECT SUM(count) FROM($resFinalQuery)cnt";

$resFinalQuery content:

(SELECT 'schema1' as 'Schema', MIN(concat(DATEIN, ' ', HOURIN)) as DATEHOUR, COUNT(*) as counting FROM schema1.calls WHERE STATE = 17 AND LEVEL = 1)
   UNION ALL
(SELECT 'schema2' as 'Schema', MIN(concat(DATEIN, ' ', HOURIN)) as DATEHOUR, COUNT(*) as counting FROM schema2.calls WHERE STATE = 17 AND LEVEL = 1)

EDIT: I solved this thank you, i had done the check before but forgot to put my code inside the if brackets.

  • Echo your query. The syntax is incorrect. I have no idea what `($resFinalQuery)cnt` is supposed to be, but you're missing at least one space – aynber May 17 '21 at 14:59
  • I would guess you do not set `$resFinalQuery` in the right scope. For example, you may be setting it in a PHP function, so it is lost when that function returns. Or you may be setting it in a previous PHP request. – Bill Karwin May 17 '21 at 15:09

1 Answers1

0

The error message shows

SELECT SUM(counting) FROM()cnt

So $resFinalQuer is empty

Assumeing that $resFinalQuer is a subquery the query muat look like

SELECT SUM(count) FROM ($resFinalQuery) cnt

which results in

SELECT SUM(count) FROM ((SELECT 'schema1' as 'Schema', MIN(concat(DATEIN, ' ', HOURIN)) as DATEHOUR, COUNT(*) as counting FROM schema1.calls WHERE STATE = 17 AND LEVEL = 1)
   UNION ALL
(SELECT 'schema2' as 'Schema', MIN(concat(DATEIN, ' ', HOURIN)) as DATEHOUR, COUNT(*) as counting FROM schema2.calls WHERE STATE = 17 AND LEVEL = 1)) cnt
nbk
  • 45,398
  • 8
  • 30
  • 47