0

How could I query this in php?

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'MAX(IF(property_name = ''',
      property_name,
      ''', value, NULL)) AS ',
      property_name
    )
  ) INTO @sql
FROM properties;
SET @sql = CONCAT('SELECT item_id, ', @sql, ' FROM properties GROUP BY item_id');

I tried to copy it and got an error, it seems that I didn't do it properly..

  • WHat error, show us what you did in PHP – RiggsFolly Jan 23 '16 at 12:32
  • 2
    Possible duplicate of [mysql\_fetch\_array() expects parameter 1 to be resource (or mysqli\_result), boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result-boole) – Pathik Vejani Jan 23 '16 at 12:34
  • MySql query in PHP : mysql_fetch_array() expects parameter 1 to be resource –  Jan 23 '16 at 12:34
  • and this is what I did `$sqlquery = mysql_query("SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT( 'MAX(IF(property_name = ''', property_name, ''', value, NULL)) AS ', property_name ) ) INTO @sql FROM properties; SET @sql = CONCAT('SELECT item_id, ', @sql, ' FROM properties GROUP BY item_id')");` –  Jan 23 '16 at 12:35
  • Why don't you do this in stored procedure. means prepare a query in stored procedure then execute it. – Abhishek Ginani Jan 23 '16 at 12:36
  • Note: The `mysql_*` functions are deprecated, they have been removed from PHP 7, your code will stop working when you upgrade to that version. You should not write new code using them, use [`mysqli_*` or PDO](http://php.net/manual/en/mysqlinfo.api.choosing.php) instead. – Gerald Schneider Jan 23 '16 at 13:03
  • you can't run multiple queries in a single `mysql_query()` call by default (it can be forced to do it though). You should switch to a newer API though, your query will work there. – Gerald Schneider Jan 23 '16 at 13:07

0 Answers0