简体   繁体   English

PDO结果显示先前查询的结果,如何显示当前结果?

[英]PDO results showing results of previous query, how to display current results?

I have two queries however the pdo->results are showing the original results and not the next results. 我有两个查询,但是pdo->结果显示的是原始结果而不是下一个结果。 I want results query_1 = (a1, a2, a3) and then query_2 = (b1, b2, b3). 我想要结果query_1 =(a1,a2,a3)然后query_2 =(b1,b2,b3)。 But the results are showing a1, a2, a3 and then a1, a2, a3 again. 但结果再次显示a1,a2,a3和a1,a2,a3。

DbGlobal.php: DbGlobal.php:

class DbGlobal 
{
  private static $_instance = null;

  private $_pdo,
      $_query,
      $_error = false,
      $_results,
      $_count = 0;

  private function __construct()
  {
    try 
    {
        $this->_pdo = new PDO('mysql:host='. ConfigGlobal::get('mysql/host') .';dbname='. ConfigGlobal::get('mysql/db'), ConfigGlobal::get('mysql/username'), ConfigGlobal::get('mysql/password'));
        //$this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } 
    catch (PDOException $e) 
    {
        die($e->getMessage());
    }
  }

  public static function getInstance()
  {
    if(!isset(self::$_instance)) 
    {
        self::$_instance = new DbGlobal();
    }
    return self::$_instance;
  }

  public function query($sql, $params = array())
  {
    $this->_error = false;

    if($this->_query = $this->_pdo->prepare($sql)) 
    {
      $x = 1;
      if(count($params)) 
      {
        foreach($params as $param) 
        {
          $this->_query->bindValue($x, $param);
          $x++;
        }
      }
      if($this->_query->execute()) 
      {
        $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
        $this->_count = $this->_query->rowCount();
      } 
      else 
      {
        $this->_error = true;
      }
    }
    return $this;
  }
}

QueryPage_1.php QueryPage_1.php

$varname = 'Settings_SideBarButtons';
$vararray = array("UniqueId,", "Url,", "Dirr,");
$varstring = implode($vararray);
${$varname} = DbGlobal::getInstance()->query("SELECT ".$varstring." FROM 
".$varname." WHERE a='1'");

In database: 在数据库中:

UniqueId => a1
Url => a2
Dirr => a3

Results are correct: 结果是正确的:

print_r($Settings_SideBarButtons->results());
//output:
Array 
( [0] => stdClass Object 
    ( [UniqueId] => a1 
      [Url] => a2 
      [Dirr] => a3
    ) 
) 

And then I have in the same query script with another query that has a different varname. 然后我在同一个查询脚本中使用另一个具有不同varname的查询。

QueryPage_2.php QueryPage_2.php

$varname = 'Core_Settings';
$vararray = array("UniqueId,", "Url,", "Dirr,");
$varstring = implode($vararray);
${$varname} = DbGlobal::getInstance()->query("SELECT ".$varstring." FROM 
".$varname." WHERE a='1'");

In database: 在数据库中:

UniqueId => b1
Url => b2
Dirr => b3

But the Results are: 但结果是:

print_r($Core_Settings->results());
//output:
Array 
( [0] => stdClass Object 
    ( [UniqueId] => a1 
      [Url] => a2 
      [Dirr] => a3
    ) 
) 

I have confirmed that the results displayed in the second query are the results from the first query. 我已确认第二个查询中显示的结果是第一个查询的结果。
Am I using PDO object incorrectly? 我是否错误地使用PDO对象? Do I have to PDO->freeresult(); 我需要PDO-> freeresult(); somehow? 不知何故?

Woops. Woops。 Easy mistake: I had an extra database field in the second $vararray that was not present in the database. 容易犯错:我在第二个$ vararray中有一个额外的数据库字段,数据库中没有。 The PDO error wasn't printing because it had been commented out in my query statement class. PDO错误未打印,因为它已在我的查询语句类中注释掉。

So remember to turn on error reporting guys. 所以记得打开错误报告人员。

Here's my PDO error fix to my code above: 这是我上面的代码的PDO错误修复:

private function __construct()
{
    try 
    {
        $this->_pdo = new PDO('mysql:host='. ConfigGlobal::get('mysql/host') .';dbname='. ConfigGlobal::get('mysql/db'), ConfigGlobal::get('mysql/username'), ConfigGlobal::get('mysql/password'));
        $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //added this
    } 
    catch (PDOException $e) 
    {
        die($e->getMessage());
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM