【问题标题】:SQLSTATE[IMSSP]: The active result for the query contains no fieldsSQLSTATE[IMSSP]:查询的活动结果不包含任何字段
【发布时间】:2019-12-18 08:23:24
【问题描述】:

我在使用 PDO 在 SQL Server 上执行一些插入查询的 PHP 脚本中收到以下错误。

SQLSTATE[IMSSP]:查询的活动结果不包含任何字段。

我不使用任何存储过程,并在查询中附加

SET NOCOUNT ON

...也无济于事。

代码似乎已经按预期插入了所有记录,但错误消息让我感到困惑。

这是一个简化的代码,应要求...

<?php

    $pdo = new PDO('sqlsrv:Server=SVR;Database=app', 'app', 'pass', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]);

    try {
        $stmt = $pdo->prepare('SELECT id FROM nation');
        $stmt->execute();
        while ($result = $stmt->fetch(PDO::FETCH_COLUMN)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://somegame.com/api/nation/id=$result&key=myapikey");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $nation = curl_exec($ch);

            $json = $nation;
            $nation = json_decode($nation, true);

            $stmt = $pdo->prepare("INSERT INTO nation_record(nation_id,as_on,json) VALUES (?,?,?,?)");
            $stmt->execute([ $result, date("Y-m-d"), $json ]);
        }
    } catch (PDOException $e) {
        api_log($pdo, $e->getMessage());
    }

    api_log($pdo, 'Completed successfully!');


    function api_log($pdo, $desc) {
        $stmt = $pdo->prepare("INSERT INTO api_log(calling_api, description) VALUES (?,?)");

        $stmt->execute([ 'myscript', $desc ]);
    }

【问题讨论】:

  • 请发布您的代码。谢谢。
  • @Zhorov 完成编辑

标签: php sql-server pdo


【解决方案1】:

考虑以下几点:

  • 错误的原因是您在SELECTINSERT 语句中使用了一个变量$stmt,在第一个INSERT 语句之后,while ($result = $stmt-&gt;fetch(PDO::FETCH_COLUMN)) ... 生成了错误。为 INSERT 语句使用不同的变量。
  • INSERT 语句在prepare() 中有四个参数占位符,但在execute() 中只有三个值。
  • 使用PDOStatement::fetchColumn 连续返回一列。

代码:

<?php

    ...
    while ($result = $stmt->fetchColumn(0)) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://somegame.com/api/nation/id=$result&key=myapikey");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $nation = curl_exec($ch);

        $json = $nation;
        $nation = json_decode($nation, true);

        $stmt2 = $pdo->prepare("INSERT INTO nation_record(nation_id,as_on,json) VALUES (?,?,?)");
        $stmt2->execute([$result, date("Y-m-d"), $json ]);
    }

...
?>

【讨论】:

  • 我重命名了后续的stmt。参数不正确,因为我简化了原始代码。
猜你喜欢
  • 2013-07-11
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多