簡體   English   中英

致命錯誤:未捕獲的 PDOException:SQLSTATE [HY000]:一般錯誤:2014 存在未決結果集時無法執行查詢

[英]Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while there are pending result sets

$query = "CALL GetAllCategories()";
$stmt = $pdo->prepare($query);
$stmt->execute();
$categories = $stmt->fetchAll();

我使用此代碼使用存儲過程從數據庫中獲取類別,沒有問題,一切正常。

這是 GetAllCategories() 程序:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `GetAllCategories`()
BEGIN
    SELECT
        id, name
    FROM
        categories ;
END$$
DELIMITER ;

之后,我使用常規的 SQL 語句來獲取帖子(為了測試,我將使用存儲過程)

這是獲取帖子的查詢:

SELECT
    `p`.`id`,
    `u`.`name` AS `author`,
    `post_title`,
    `post_date`,
    `post_img`,
    `post_content`
FROM
    `posts` `p`
JOIN `users` `u` ON
    `p`.`author_id` = `u`.`id`;

從這里問題發生了

錯誤圖像

出現此錯誤:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while there are pending result sets. Consider unsetting the previous PDOStatement or calling PDOStatement::closeCursor() in C:\xampp\htdocs\cms\index.php:17 Stack trace: #0 C:\xampp\htdocs\cms\index.php(17): PDO->prepare('SELECT\n `p`....') #1 {main} thrown in C:\xampp\htdocs\cms\index.php on line 17

在嘗試多次嘗試時,我將GetAllCategories()存儲過程更改為常規查詢語句:

$query = "SELECT * FROM `categories`";

問題消失了

誰能解釋為什么這與存儲過程有關?

最后一件事,在這個issue之后,從PHPMyAdmin瀏覽posts表時又出現了一個問題

帖子表瀏覽錯誤

我用$stmt->closeCursor(); GetAllCategories()存儲過程中獲取數據以解決此錯誤后。

$query = "CALL GetAllCategories()";
$stmt = $pdo->prepare($query);
$stmt->execute();
$categories = $stmt->fetchAll();
$stmt->closeCursor();

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while there are pending result sets. Consider unsetting the previous PDOStatement

當同時執行兩次執行時,總是會發生此錯誤,例如:

  1. 情況1

    如果(!$查詢->執行())

     die("SQL FAILED");

    別的

    $query->execute(); // **DELETE THIS ONE**

解決方案:不需要第二次執行,刪除第二次,第一次執行已經執行。

  1. 如果第一種情況不起作用,請使用此解決方案在$query=$handler->prepare($sql); 之后調用此 function;

    $query->closeCursor();

我希望這個解決方案有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM