0

I have a query, I want to write it in php but its showing error.

My query is like below:

$exe1 = mysql_query("
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'SUM(CASE WHEN `size_name` = ''',
      `size_name`,
      ''' THEN Quantity ELSE 0 END) AS `',
      `size_name`, '`'
    )
  ) INTO @sql
FROM Size;

SET @sql = CONCAT('SELECT st.Style , ', @sql, '
                  FROM Size s
                  JOIN Style st ON s.style_id = st.id
                   GROUP BY st.Style 
                  ');

PREPARE stmt FROM @sql;
EXECUTE stmt;") or die(mysql_error());

But its showing the message

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 'SET @sql = CONCAT('SELECT st.Style , ', @sql, ' FROM Size s ' at line 12

I just dont know how to solve this error. Can anyone please help me to solve this problem?

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Amit Chowdhury
  • 623
  • 1
  • 7
  • 22

2 Answers2

0

You should print out the resulting query in @sql to see the problem.

In your case, you would seem to have an extra comma after the last argument. Try changing the line:

SET @sql = CONCAT('SELECT st.Style , ', @sql, '
                  FROM Size s
                  JOIN Style st ON s.style_id = st.id
                   GROUP BY st.Style 
                  ');

to:

SET @sql = CONCAT('SELECT st.Style , ', @sql'
                  FROM Size s
                  JOIN Style st ON s.style_id = st.id
                   GROUP BY st.Style 
                  ');
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

mysql_query does not support multiple queries.

http://php.net/manual/en/function.mysql-query.php

mysql_* functions are deprecated and will be deleted. I advise you to use mysqli_* functions or PDO.

roptch
  • 225
  • 1
  • 6