SQLSTATE [HY000]:一般错误

时间:2012-11-05 03:09:26

标签: php mysql sql pdo dynamic-sql

我有一个mysql dynamic-sql语句,我想用pdo运行它,我收到以下错误信息:

  

警告:PDOStatement :: fetchAll()[pdostatement.fetchall]:   SQLSTATE [HY000]:C:\ xampp \ htdocs \ tags \ cobasql.php中的常规错误   第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;
?>

这里有人可以帮我或者给我一个建议吗?

1 个答案:

答案 0 :(得分:-1)

好像你连接不了!

这一行

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

尝试删除$ db name var并输入正确的值,使您的行看起来像

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