关于phpmyadmin的声明工作,但不是通过php

时间:2012-11-05 07:24:03

标签: php mysql pdo dynamic-sql multiple-select-query

  

可能重复:
  PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)

我在这里有一个声明..当我从phpmyadmin运行它...我得到了我的查询工作..但它不是当我从PHP执行它..

这是我的查询(声明)

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;

这是我在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);
?>

任何人都可以帮助我或在这里给我一个建议吗?

2 个答案:

答案 0 :(得分:2)

此问题出现在PDO之前。不确定它是否是错误或安全功能(可能与此相关:Mysql can't perform more than 1 query at a time)。

看一下这个解决方案:http://www.php.net/manual/en/book.pdo.php#96264

答案 1 :(得分:0)

我认为PDO无法执行多个查询。尝试在此使用Stored Procedure

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 ;

并在PHP文件上调用procName