简体   繁体   English

如何使用临时表获取查询以在PHP中工作?

[英]How to get query with temp table to work in PHP?

I'm simply trying this query in PHP for the temp table, but it's not working in PHP. 我只是尝试在PHP中针对临时表进行此查询,但在PHP中不起作用。

IF OBJECT_ID('tempdb..##t1') IS NOT NULL
    DROP TABLE ##t1
select 'rec1' as col1 into ##t1;
select * from ##t1

PHP code: PHP代码:

$database = new SQL_DB;
$database->query(
  "
  IF OBJECT_ID('tempdb..##t1') IS NOT NULL
    DROP TABLE ##t1

  SELECT 'rec1' as col1 into ##t1;
  SELECT * FROM ##t1;
  "
);
$rows = $database->resultset();

SQL_DB class (simplified): SQL_DB类(简体):

public function __construct(){
    //Set DSN
    $dsn = 'sqlsrv:Server=' . $this->host . ';Database=' . $this->dbname;
    //Set options
    /*$options = array(
        PDO::ATTR_EMULATE_PREPARES => true,
        PDO::SQLSRV_ATTR_ENCODING => PDO::SQLSRV_ENCODING_UTF8
    );*/
    $options = array(
      PDO::SQLSRV_ATTR_QUERY_TIMEOUT => $this->TIMEOUT,
      PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION,
      PDO::ATTR_EMULATE_PREPARES => false
    );
    //Create a new PDO instance
    try {
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
        // $this->dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    } catch (PDOException $e) {
        $this->error = $e->getMessage();
    }
}

//Prepare statement
public function query($query) {
    $this->stmt = $this->dbh->prepare($query);
}

//Return result Set
public function resultset($in = "") {
  try{
    if ($in == ""){
      $this->stmt->execute();
    }
    else{
      $this->stmt->execute($in);
    }
    return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  }
  catch (PDOException $e) {
      $this->error = $e->getMessage();
  }
}

It works just fine in Microsost SQL Server Management Studio and all other queries I've worked with in PHP work just fine so it's not any of the connection problem. 在Microsost SQL Server Management Studio中它可以正常工作,而我在PHP中使用过的所有其他查询都可以正常工作,因此它不是任何连接问题。
It definitely has something to do with the temp table and '#'. 它肯定与临时表和“#”有关。

What am I missing? 我想念什么?

Update: 更新:

It works just fine if I split the query into 2 parts: 如果将查询分为两部分,它就可以正常工作:

$database = new SQL_DB;
$database->query(
  "
  IF OBJECT_ID('tempdb..##t1') IS NOT NULL
      DROP TABLE ##t1
  SELECT 'rec1' as col1 into ##t1;
"
);
$database->resultset();
$database->query(
  "
  SELECT * from ##t1;
"
);
$rows = $database->resultset();

Turns out the problem isn't the temp table but how PDO works for multiple result sets. 原来问题不是临时表,而是PDO如何为多个结果集工作。
The fix is simply changing the way the result is fetched (in this case fetching the last result set): 解决方法只是更改获取结果的方式(在本例中为获取最后一个结果集):

public function lastresultset($in = "") {
  try{
    if ($in == ""){
      $this->stmt->execute();
    }
    else{
      $this->stmt->execute($in);
    }
    $result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    while($this->stmt->nextRowset()){
      $result = $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    };
    return $result;
  }
  catch (PDOException $e) {
      $this->error = $e->getMessage();
  }
}

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

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