语法错误或访问冲突:1064-PDO拒绝执行多个查询(包括CREATE TEMPORARY TABLE)

时间:2019-05-08 00:33:33

标签: php mysql pdo

我正在尝试使用PHP中的PDO执行以下查询。在PhpMyAdmin中应用该查询时,该查询工作正常且没有任何提及的问题。但是,在PHP中执行它时会显示以下错误:

 Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'CREATE TEMPORARY TABLE IF NOT EXISTS temp2 AS SELECT COUNT(*) AS total_visits, s' at line 1

代码很简单,我不确定是否需要表结构,但是会在问题末尾提供

    $sql = "CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS SELECT COUNT(*) AS total_views, series_id FROM my_app_stats d1 WHERE 1 = 1 AND d1.stats_type = 0 AND d1.created_at_text = DATE_FORMAT(NOW(), '%Y-%m-%d') GROUP BY series_id; CREATE TEMPORARY TABLE IF NOT EXISTS temp2 AS SELECT COUNT(*) AS total_visits, series_id FROM my_app_stats d2 WHERE 1 = 1 AND d2.stats_type = 1 AND d2.created_at_text = DATE_FORMAT(NOW(), '%Y-%m-%d') GROUP BY series_id; ALTER TABLE `temp1` ADD INDEX `temp1_idx_id` (`series_id`); ALTER TABLE `temp2` ADD INDEX `temp2_idx_id` (`series_id`); SELECT database_series.id, (SELECT dusers.user_name FROM my_app_users dusers WHERE dusers.id = database_series.series_owner) user_name, database_series.series_title, temp1.total_views, temp2.total_visits FROM my_app_series database_series LEFT JOIN temp1 ON temp1.series_id = database_series.id LEFT JOIN temp2 ON temp2.series_id = database_series.id ORDER BY total_views DESC, total_visits DESC LIMIT 10";
    $query = $this->conn->prepare($sql);
    $query->execute();
    $data = $query->fetchAll(PDO::FETCH_ASSOC);
    $query->closeCursor();
    return $data;

my_app_users

id | user_name

my_app_stats

id | series_id | created_at (timestamp) | created_at_text (text, formatted as y-m-d) 

my_app_series

id | series_owner

1 个答案:

答案 0 :(得分:1)

您不能将prepare()与一系列多个语句一起使用。 MySQL不支持它。

这几乎是PHP MySQLi multi_query prepared statement的副本,但这是针对mysqli的。没关系,这是MySQL的限制,而不是PHP接口的限制。

无论如何,没有理由在一个调用中准备多个SQL语句。

您应该一次执行一条语句。

关于您的评论:是的,它们都是相关的,只要您使用$this->conn中具有相同连接的查询表,就可以依靠剩余的临时表。除非您关闭连接(或者故意删除临时表,否则临时表不会消失)。

示例:

$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS temp1 AS 
  SELECT COUNT(*) AS total_views, series_id FROM my_app_stats d1 
  WHERE 1 = 1 AND d1.stats_type = 0 
    AND d1.created_at_text = DATE_FORMAT(NOW(), '%Y-%m-%d') 
  GROUP BY series_id";
$this->conn->exec($sql);

$sql = "CREATE TEMPORARY TABLE IF NOT EXISTS temp2 AS 
  SELECT COUNT(*) AS total_visits, series_id FROM my_app_stats d2 
  WHERE 1 = 1 AND d2.stats_type = 1 
    AND d2.created_at_text = DATE_FORMAT(NOW(), '%Y-%m-%d') 
  GROUP BY series_id";
$this->conn->exec($sql);

$sql = "ALTER TABLE `temp1` ADD INDEX `temp1_idx_id` (`series_id`)";
$this->conn->exec($sql);

$sql = "ALTER TABLE `temp2` ADD INDEX `temp2_idx_id` (`series_id`)";
$this->conn->exec($sql);

$sql = "SELECT 
    database_series.id,
    (SELECT dusers.user_name FROM my_app_users dusers 
     WHERE dusers.id = database_series.series_owner) AS user_name, 
    database_series.series_title, 
    temp1.total_views, 
    temp2.total_visits 
  FROM my_app_series database_series 
  LEFT JOIN temp1 ON temp1.series_id = database_series.id 
  LEFT JOIN temp2 ON temp2.series_id = database_series.id 
  ORDER BY total_views DESC, total_visits DESC LIMIT 10";
$query = $this->conn->prepare($sql);
$query->execute();
$data = $query->fetchAll(PDO::FETCH_ASSOC);
return $data;