donglu9872 2019-07-04 08:04
浏览 130

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

I'm simply trying this query in PHP for the temp table, but it's not working in PHP.

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

PHP code:

$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):

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.
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();
  • 写回答

1条回答 默认 最新

  • dqvtm82066 2019-07-05 01:59
    关注

    Turns out the problem isn't the temp table but how PDO works for multiple result sets.
    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();
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序