2

我有一个 mysql 动态 sql 语句,我想使用 pdo 运行它,我收到以下错误消息:

警告:PDOStatement::fetchAll() [pdostatement.fetchall]: SQLSTATE[HY000]: General error in C:\xampp\htdocs\tags\cobasql.php on line 29

这是我的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);
   echo $result_array;
?>

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

4

1 回答 1

-1

它看起来像你无法连接!

这条线在这里

$db = new PDO('mysql:host=localhost;dbname='.$dbname, $uname, $pwd);

尝试删除 $dbname 变量并输入正确的值,使您的行看起来像

 $db = new PDO('mysql:host=localhost;dbname=dbname', $uname, $pwd);
于 2012-11-05T03:22:26.680 回答