我的PHP脚本中出现以下错误,该脚本使用PDO在SQL Server上执行一些插入查询。
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 ]);
}
答案 0 :(得分:0)
请考虑以下内容:
$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 ]);
}
...
?>