简体   繁体   English

PHP:致命错误:未捕获的 PDOException:SQLSTATE[HY000]

[英]PHP: Fatal error: Uncaught PDOException: SQLSTATE[HY000]

I have a class named player that creates, deletes and controls if a player entity exists.我有一个名为 player 的类,用于创建、删除和控制玩家实体是否存在。 In another file, I have the database connection with PDO and, finally, in third file I have the call to player class.在另一个文件中,我与PDO建立了数据库连接,最后,在第三个文件中,我调用了播放器类。 Here's all the code:这是所有的代码:

file: player.php文件: player.php

<?php
    class player
    {
        private $pdo;
        private $network_id;
        public $color;

        public function __construct($pdo, $network_id, $color)
        {
            $this->pdo = $pdo;
            $this->network_id = $network_id;
            $this->color = $color;
        }

        public function create_player()
        {
            if(!$this->exists_player())
            {
                $sql = 'INSERT INTO players SET network_id = :network_id';
                $query = $this->pdo->prepare($sql);
                $query->execute(array(':network_id' => $this->network_id));
            }
            else
            {
                echo 'error';
            }
        }

        public function delete_player()
        {
            if($this->exists_player())
            {
                $sql = 'DELETE FROM players WHERE network_id = :network_id';
                $query = $this->pdo->prepare($sql);
                $query->execute(array(':network_id' => $this->network_id));
            }
            else
            {
                return -1;
            }
        }

        private function exists_player()
        {
            $sql = 'SELECT COUNT(*) FROM players WHERE network_id = '.$this->network_id;
            $result = $this->pdo->exec($sql);

            if($result > 0) return true;
            else return false;
        }
    }
?>

file: test.php文件: test.php

<?php
   include './Php/db_connection.php';
   include './Php/player.php';

    $player = new player($pdo, 1112, 'red');

    $player->create_player();
?>

file: db_connection.php文件: db_connection.php

 $pdo = new PDO('mysql:host=localhost; dbname=myDbName', 'dbUtent', 'myPassword');
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    

The thing is that when I call test.php , I get this error:问题是当我调用test.php 时,我收到此错误:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in F:\\Software\\Coding_Development_Software\\Server\\wamp\\www\\myProject\\Ajax\\Cube\\Php\\player.php on line 21

All code is an exemple.所有代码都是一个例子。

Any ideas?有任何想法吗?

The thing here is that you need to fetch until it fails for a row fetch attempt.这里的问题是您需要获取直到行获取尝试失败为止。 In fact, your own exception is telling you the solution:事实上,您自己的例外正在告诉您解决方案:

Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. in F:\\Software\\Coding_Development_Software\\Server\\wamp\\www\\myProject\\Ajax\\Cube\\Php\\player.php on line 21

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:未捕获的 PDOException:SQLSTATE[HY000]:一般错误 - Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error Php 致命错误:未捕获的 PDOException:SQLSTATE[HY000] [1045] 用户 &#39;fobos&#39;@&#39;localhost&#39; 访问被拒绝(使用密码:YES) - Php Fatal error: Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'fobos'@'localhost' (using password: YES) 致命错误:未捕获异常&#39;PDOException&#39;,消息&#39;SQLSTATE [HY000]:常规错误&#39;中 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in 致命错误:未被捕获的PDOException:SQLSTATE [HY000]:常规错误:提取模式要求在 - Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: fetch mode requires the classname argument in 致命错误:带有消息“ SQLSTATE [HY000]:常规错误”的未捕获异常“ PDOException” .. C:\\ xampp \\ .. PDOStatement-&gt; fetch()..第83行 - Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' .. C:\xampp\..PDOStatement->fetch().. on line 83 致命错误:未捕获的 PDOException:SQLSTATE [HY000]:一般错误:2014 存在未决结果集时无法执行查询 - Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while there are pending result sets 未捕获的异常&#39;PDOException,消息为&#39;SQLSTATE [HY000] [2013] - Uncaught exception 'PDOException with message 'SQLSTATE[HY000] [2013] PHP 致命错误:未捕获的 PDOException:SQLSTATE [HY093] - PHP Fatal error: Uncaught PDOException: SQLSTATE[HY093] 由于“双端口”,PHP PDO初始化失败 - 未捕获PDOException:SQLSTATE [HY000] [2002] - PHP PDO initialization fails due to “double port” - Uncaught PDOException: SQLSTATE[HY000] [2002] 邮递员:未捕获的PDOException:SQLSTATE [HY000]:常规错误:1366不正确的整数值:&#39;&#39; - Postman: Uncaught PDOException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM