PHP5 MySql和PDO异常
我在PHP5.4和MySql中使用PDO时遇到了一个异常 我已经阅读了几乎所有其他的帖子、博客、手册、片段,以及所有与此错误相关的内容,并且尝试了所有我所看到的内容,但都没有成功 所以我希望这是我在我的特定代码中做错的事情,你可以向我指出 以下是错误: SQLSTATE[HY000]:一般错误:当其他未缓冲查询处于活动状态时,2014无法执行查询。考虑使用pdoCalp::FutAccess()。或者,如果您的代码只在mysql上运行,您可以通过设置PDO::mysql_ATTR_USE_BUFFERED_query属性来启用查询缓冲 以下是导致此错误的代码部分: 第一部分:PHP5 MySql和PDO异常,php,mysql,pdo,Php,Mysql,Pdo,我在PHP5.4和MySql中使用PDO时遇到了一个异常 我已经阅读了几乎所有其他的帖子、博客、手册、片段,以及所有与此错误相关的内容,并且尝试了所有我所看到的内容,但都没有成功 所以我希望这是我在我的特定代码中做错的事情,你可以向我指出 以下是错误: SQLSTATE[HY000]:一般错误:当其他未缓冲查询处于活动状态时,2014无法执行查询。考虑使用pdoCalp::FutAccess()。或者,如果您的代码只在mysql上运行,您可以通过设置PDO::mysql_ATTR_USE_BUF
$stmt = $this->dbConn->prepare("CALL getPopularProducts()");
$stmt->execute();
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
//$stmt->closeCursor();
foreach ($rows as $row) {
$p = new entity\LightingPole();
/* doing basic set stuff here so code omitted */
// apply category to product
$categroyTranslator = new ProductCategoryTranslator();
$categroyTranslator->applyProductCategory($p);
public function applyProductCategory(entity\Product $product) {
$productId = $product->getProductId();
try {
/** exception thrown on this line **/
$stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)");
$stmt->bindParam(1, $productId, \PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
$category = new entity\ProductCategory();
$product->setCategory($category);
}
}
catch (\PDOException $e) {
echo $e->getMessage();
print($e->getTraceAsString());
}
}
namespace lib\database;
class Database
{
protected static $instance;
public static function getInstance()
{
if(self::$instance == null)
{
$dsn = "mydsn";
$user = "myuser";
$password = "mypassword";
self::$instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
self::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
self::$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __construct() {}
private function __clone() {}
}
第二部分:
$stmt = $this->dbConn->prepare("CALL getPopularProducts()");
$stmt->execute();
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
//$stmt->closeCursor();
foreach ($rows as $row) {
$p = new entity\LightingPole();
/* doing basic set stuff here so code omitted */
// apply category to product
$categroyTranslator = new ProductCategoryTranslator();
$categroyTranslator->applyProductCategory($p);
public function applyProductCategory(entity\Product $product) {
$productId = $product->getProductId();
try {
/** exception thrown on this line **/
$stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)");
$stmt->bindParam(1, $productId, \PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
$category = new entity\ProductCategory();
$product->setCategory($category);
}
}
catch (\PDOException $e) {
echo $e->getMessage();
print($e->getTraceAsString());
}
}
namespace lib\database;
class Database
{
protected static $instance;
public static function getInstance()
{
if(self::$instance == null)
{
$dsn = "mydsn";
$user = "myuser";
$password = "mypassword";
self::$instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
self::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
self::$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __construct() {}
private function __clone() {}
}
这是我的数据库类:
$stmt = $this->dbConn->prepare("CALL getPopularProducts()");
$stmt->execute();
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
//$stmt->closeCursor();
foreach ($rows as $row) {
$p = new entity\LightingPole();
/* doing basic set stuff here so code omitted */
// apply category to product
$categroyTranslator = new ProductCategoryTranslator();
$categroyTranslator->applyProductCategory($p);
public function applyProductCategory(entity\Product $product) {
$productId = $product->getProductId();
try {
/** exception thrown on this line **/
$stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)");
$stmt->bindParam(1, $productId, \PDO::PARAM_INT);
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)){
$category = new entity\ProductCategory();
$product->setCategory($category);
}
}
catch (\PDOException $e) {
echo $e->getMessage();
print($e->getTraceAsString());
}
}
namespace lib\database;
class Database
{
protected static $instance;
public static function getInstance()
{
if(self::$instance == null)
{
$dsn = "mydsn";
$user = "myuser";
$password = "mypassword";
self::$instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
self::$instance->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
self::$instance->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
private function __construct() {}
private function __clone() {}
}
您可能会注意到我对$stmt->closeCursor()
的调用已被注释掉。当我包含这行代码时,我得到以下错误:
SQLSTATE[HY000]:一般错误:2006 MySQL服务器已经离开
现在用简单的功能术语来解释它,我在这里主要做的是获取流行产品的列表,循环浏览它们,并获取所有相关的产品数据,如ProductCategory等
因此,这个错误只发生在循环中的第一个产品上,即第二次调用applyProductCategory()
时,它工作正常,没有问题。另一件事是,下面的applyProductCategory
还有大约4个调用用于检索其他类型的关联数据,并且在执行此操作时也不会抛出任何错误
所以最后,我所有受欢迎的产品都很好,除了第一个产品从来没有与之关联的ProductCategory
期待着发现我在这一过程中错过了什么。希望您能给我指出正确的方向。在抛出错误之前尝试使用unset($stmt)
unset($stmt);
$stmt = $this->dbConn->prepare("CALL getCategoryByProductId(?)");
我以前也遇到过同样的错误,出于某种原因,这就解决了它。我以前在这里找到了答案@Erik,我不是在逃避PDO,我使用的是php名称空间,斜杠是必需的,因此php不会在我的类中寻找PDO。酷!您每天都会学到一些新东西:)请注意,我将unset($stmt)直接放在$rows=fetchAll()之后,因为错误被抛出到一个单独的类中,所以$stmt此时在一个新范围中。