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

问题描述 投票:0回答:1

我在使用 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 ]);
    }
php sql-server pdo sqlsrv
1个回答
1
投票

考虑以下因素:

  • 错误的原因是您在
    $stmt
    SELECT
    语句中使用了一个变量
    INSERT
    ,并且在第一个
    INSERT
    语句之后,
    while ($result = $stmt->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 ]);
    }

...
?>
© www.soinside.com 2019 - 2024. All rights reserved.