3

Possible Duplicate:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

i have a statement here.. when i ran it from phpmyadmin.. i got my query works.. but it doesnt when i execute it from php..

here's my query (statement)

SET @sql = NULL;
 SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(a.Name = ''', a.Name, ''', ca.value, NULL)) AS ', a.Name )) INTO @sql FROM cust_attribute ca INNER JOIN attribute a ON ca.idAttribute = a.idAttribute INNER JOIN customer c ON c.idCustomer = ca.idCustomer WHERE a.publish = 'Y';
 SET @sql = CONCAT('SELECT c.idCustomer, c.namaCustomer, ' ' c.email, c.noTelp, ', @sql, ' FROM cust_attribute ca', ' INNER JOIN attribute a ON ca.idAttribute = a.idAttribute', ' INNER JOIN customer c ON c.idCustomer = ca.idCustomer', ' GROUP BY c.idCustomer');
 SELECT @sql;
 prepare stmt FROM @sql;
 execute stmt;

and here's my code on php...

<?php 
$uname = 'root'; //nama username database 
$pwd = ''; //Password database 
$dbname = 'eav_latihan'; //nama Database 

/* Initialisasi database */ 
$db = new PDO('mysql:host=localhost;dbname='.$dbname, $uname, $pwd); 
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); 


$query = "SET @sql = NULL; ". 
" SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(a.Name = ''', ". 
" a.Name, ''', ca.value, NULL)) AS ', a.Name )) INTO @sql ". 
" FROM cust_attribute ca ". 
" INNER JOIN attribute a ON ca.idAttribute = a.idAttribute ". 
" INNER JOIN customer c ON c.idCustomer = ca.idCustomer ". 
" WHERE a.publish = 'Y'; ". 
" SET @sql = CONCAT('SELECT c.idCustomer, c.namaCustomer, ' ". 
" ' c.email, c.noTelp, ', @sql, ' FROM cust_attribute ca', ". 
" ' INNER JOIN attribute a ON ca.idAttribute = a.idAttribute', ". 
" ' INNER JOIN customer c ON c.idCustomer = ca.idCustomer', ". 
" ' GROUP BY c.idCustomer'); ". 
" SELECT @sql; ". 
" prepare stmt FROM @sql; ". 
" execute stmt;"; 

$result_array = array();
$queryku = $db->query($query);
$result_array = $queryku->fetchAll(PDO::FETCH_OBJ);
var_dump($result_array);
?>

Anyone can help me or give me a suggest here??

2
  • use a try-catch block, catch all exceptions that are raised and look at the messages they produce. Also: why are you using session variables and db-side prepared statements? PS: look into how to turn of emulated prepared statements in the PDO extension, because that might be what is causing your problem, that and the fact that half of the time you're using too many single quotes: a.Name = ''' Commented Nov 5, 2012 at 7:35
  • 4 years in the future and wondering how you sloved this? Commented Feb 12, 2016 at 21:06

2 Answers 2

2

This issue has come up before with regards to PDO. Not sure if it's a bug or a security feature (might be related to this: Mysql can't perform more than 1 query at a time).

Have a look at this solution: http://www.php.net/manual/en/book.pdo.php#96264

Sign up to request clarification or add additional context in comments.

Comments

0

I think PDO can't execute multiple queries. Try using a Stored Procedure on this.

DELIMITER $$
CREATE PROCEDURE procName
BEGIN
SET @sql = NULL;
 SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(a.Name = ''', a.Name, ''', ca.value, NULL)) AS ', a.Name )) INTO @sql FROM cust_attribute ca INNER JOIN attribute a ON ca.idAttribute = a.idAttribute INNER JOIN customer c ON c.idCustomer = ca.idCustomer WHERE a.publish = 'Y';
 SET @sql = CONCAT('SELECT c.idCustomer, c.namaCustomer, ' ' c.email, c.noTelp, ', @sql, ' FROM cust_attribute ca', ' INNER JOIN attribute a ON ca.idAttribute = a.idAttribute', ' INNER JOIN customer c ON c.idCustomer = ca.idCustomer', ' GROUP BY c.idCustomer');
 SELECT @sql;
 prepare stmt FROM @sql;
 execute stmt;
END $$
DELIMITER ;

and call procName on your PHP file.

1 Comment

how to do that?? i know nothing about stored procedure.. where to create that?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.