2017-03-20 306 views
1

當使用下面的代碼嘗試連接到我的SQL服務器時,我遇到了問題。一般SQL Server錯誤:檢查來自SQL Server [207](嚴重性16)的消息

$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(); 
} 

當我執行此腳本時,出現以下錯誤。

Array ([0] => HY000 [1] => 207 [2] => General SQL Server error: Check messages from the SQL Server [207] (severity 16) [] [3] => -1 [4] => 16)

如果我的print_r $胸徑我得到的只是PDO()

哪些錯誤的PDO插入。

聽起來很奇怪,和我玩了一段時間。

但同樣的pdo對象我在下面的腳本中用於選擇查詢,我得到'It is working'消息!

$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(); 
}  
+0

在你插入查詢您正在使用GMR數據庫,當您連接到'與PDO – hassan

+0

機場airport'數據庫是DB和GMR是表。 ...我改變了報價,現在插入..謝謝! – Sach

回答

0

試圖用單qoutes硬編碼值插入和它得到了插入。

因此改變在INSERT語句中的引號從

'INSERT INTO gmr VALUES (" ' . implode(' ", " ', $array) . ' ")') to

"INSERT INTO gmr VALUES (' " . implode(" ', ' ", $array) . " ')")