静态成功时,MSSQL PDO准备的选择语句失败

时间:2014-04-09 20:39:32

标签: php sql-server pdo

所以我正在开发一个同步两个数据库的脚本,我知道如果选择行的PDO查询是静态的,并且没有准备好,代码就可以工作。在更改要准备的语句并传递确定当前记录拉取请求开始的变量时,代码将失败,并显示以下错误。

错误代码:

I'm sorry, Dave. I'm afraid I can't do that.
SQLSTATE[HY000]: General error: 10743 General SQL Server error: Check messages from the SQL Server [10743] (severity 15) [(null)]

PHP代码:

<?php
$offset = 0;
try 
{
$DBH = new PDO('dblib:host=obscured;dbname=obscured', 'obscured', 'obscured');
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

if 
(
$STH = $DBH->query("SELECT COUNT(LocalSKU) FROM Inventory")
) 
{
    if ($STH->fetchColumn() > 0) 
    {
        $stmt = $DBH->query("SELECT COUNT(LocalSKU) FROM Inventory");
        $count = $stmt->fetchAll(PDO::FETCH_ASSOC);
        $maxlimit = intval($count[0]['']);
        while ($offset < $maxlimit) 
        {
            $STH = $DBH->prepare("SELECT LocalSKU, Price, Price2, Price3, Price4 FROM Inventory ORDER BY LocalSKU OFFSET ? ROWS FETCH NEXT 1000 ROWS ONLY");
            $STH->execute(array($offset));
            $pricebracket = $STH->fetchALL(PDO::FETCH_ASSOC);

            $postdata = json_encode($pricebracket);
            $post = curl_init('obscured/price2.php');
            curl_setopt($post, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($post, CURLOPT_POSTFIELDS, $postdata);
            curl_setopt($post, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($post, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postdata)));
            $result = curl_exec($post);

            if (curl_errno($post)) 
            {
                die('Could not send request: '.curl_error($post));
            } 
            else
            {
                $resultStatus = curl_getinfo($post, CURLINFO_HTTP_CODE); 

                if ($resultStatus == 200) 
                {
                    echo 'Bracket # '.$offset.'. Not as bad as it could be. HTTP Status code: '.$resultStatus.'<br />';
                } 
                else 
                {
                    die('Request failed: HTTP status code: '.$resultStatus);
                }
            }
            curl_close($post);
            $offset += 1000;
            $rows = array();
        }
    }
} 
} 
catch(PDOException $e) 
{
echo "I'm sorry, Dave. I'm afraid I can't do that.<br />";
echo $e->getMessage();
}
$DBH = null;
?>

如果?在准备好的语句中替换为0,并且该语句作为查询而不是prepare运行,或者即使作为没有通过execute传递的变量的prepare,该过程也会成功。不幸的是,它收集相同的1000条记录,并将它们卷曲13次到接收脚本,而不是记录组(分解我们拥有的13,274)并发送每个1000记录组。

敏感的登录凭据和文件路径已替换为“模糊”一词,但它们都有效。

1 个答案:

答案 0 :(得分:1)

尝试这样告诉PDO你绑定了一个整数,因为它不会在变量周围放置''

 $STH->bindParam(1, $offset, PDO::PARAM_INT)
 $STH->execute();