PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 24, 2022

[FIXED] Mysql pivot table is fullly working in PhpMyadmin but not with Php mysqli_query()

 January 24, 2022     mysql, mysqli, php, phpmyadmin, pivot     No comments   

Issue

I've this dynamic MYSQL query building a pivot table that works in phpmyadmin (and SequelPro):

SET SESSION group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
    GROUP_CONCAT(DISTINCT
    'MAX(CASE WHEN meta_key = "lat_poi" THEN meta_value END) AS lat_poi,
    MAX(CASE WHEN meta_key = "lng_poi" THEN meta_value END) AS lng_poi'
   ) INTO @sql


FROM wp_postmeta;

SET @sql = CONCAT('SELECT posts.post_title, ', @sql, '
               FROM wp_posts posts
                LEFT JOIN wp_postmeta postmeta 
                ON posts.ID = postmeta.post_id
                WHERE (posts.post_type = "poi" AND posts.post_status = "publish")
                GROUP BY posts.ID
               ');

PREPARE stmt FROM @sql;
EXECUTE stmt;

I want to use it in Php to query the database but it gives this error:

string(226) "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 = NULL; 
SELECT
   GROUP_CONCAT(DISTINCT
     'MAX(CASE WHEN meta_key = ' at line 2"
 bool(false)

Here's the Php code:

$connection = mysqli_connect("localhost", "db_name", "db_pwd", "db_user");
$query_expression = "
SET SESSION group_concat_max_len = 1000000;
SET @sql = NULL;
SELECT
   GROUP_CONCAT(DISTINCT
     'MAX(CASE WHEN meta_key = \"lat_poi\" THEN meta_value END) AS lat_poi,
      MAX(CASE WHEN meta_key = \"lng_poi\" THEN meta_value END) AS lng_poi'
) INTO @sql


   FROM wp_postmeta;

   SET @sql = CONCAT('SELECT posts.post_title, ', @sql, '
               FROM wp_posts posts
                LEFT JOIN wp_postmeta postmeta 
                ON posts.ID = postmeta.post_id
                WHERE (posts.post_type = \"poi\" AND posts.post_status = \"publish\")
                GROUP BY posts.ID
               ');

   PREPARE stmt FROM @sql;
   EXECUTE stmt;";
$query = mysqli_query($connection, $query_expression);
var_dump(mysqli_error($connection));
var_dump($query);

Any hint?

Thanks in advance...


Solution

This is not a query but a set of queries.

Split it into separate statements and run them one by one

mysqli_query($connection, "SET SESSION group_concat_max_len = 1000000");
mysqli_query($connection, "SET @sql = NULL");
$query_expression = "SELECT...";
$query = mysqli_query($connection, $query_expression);


Answered By - Your Common Sense
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing