[英]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.