常规SQL Server错误:检查来自SQL Server的消息[207](严重级16)

时间:2017-03-20 09:37:38

标签: php sql-server

尝试使用以下代码连接到我的SQL Server计算机时遇到问题。

$hostname = '10.11.22.33';                 
$username = 'siddharth';
$password = '1234';
$dbname = 'airport';
$port='1433';

$dbh = new PDO("dblib:host=$hostname:$port;dbname=$dbname;charset=UTF-8", $username, $password);
$arraykey=array_keys($data); 
$array=$data[$arraykey[0]]; 

try 
{
 //$count = $dbh->exec('INSERT INTO dbo.gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ("' . implode('", "', $array) . '")' ) or die(print_r($dbh->errorInfo(), true)); 
 // Changed from double quotes to single while inserting VALUES
 $count = $dbh->exec("INSERT INTO dbo.gmr(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ('" . implode("', '", $array) . "')" ) or die(print_r($dbh->errorInfo(), true)); 
 $dbh = null;
echo 'Data Successfully inserted!!<br />';
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

执行此脚本时出现以下错误。

  

数组([0] =&gt; HY000 [1] =&gt; 207 [2] =&gt;一般SQL Server错误:检查来自SQL Server的邮件[207](严重级16)[] [3] =&gt; ; -1 [4] =&gt; 16)

如果我print_r $ dbh,我只得到PDO()

PDO插入错误。

听起来很奇怪,和我玩了一会儿。

但是我在下面的脚本中用于选择查询的同一个pdo对象,我得到了&#39;它正在工作&#39;消息!!

$hostname = '10.11.22.33';                 
$username = 'siddharth';
$password = '1234';
$dbname = 'airport';
$port='1433';
try {
$dbh = new PDO("dblib:host=$hostname:1433;dbname=$dbname;charset=UTF-8", $username, $password);

$sql = "SELECT 'It is working' AS name";
foreach ($dbh->query($sql) as $row) {
    print $row['name'] . "\n";
}
} catch (PDOException $ex) {
print $ex->getMessage();
}    

1 个答案:

答案 0 :(得分:0)

尝试使用单个qoutes插入硬编码值并插入。

因此更改了

中insert语句中的引号
  

&#39;插入gmr值(&#34;&#39;。内爆(&#39;&#34;,&#34;&#39;,$ array)。&#39;& #34;)&#39; )到

     

&#34;插入gmr VALUES(&#39;&#34;。implode(&#34;&#39;,&#39;&#34;,$ array)。&#34;& #39)&#34; )