Im facing issue while trying to connect to my SQL server machine with the below code.
$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!!
';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I get the below error when i execute this script.
Array ( [0] => HY000 [1] => 207 [2] => General SQL Server error: Check messages from the SQL Server [207] (severity 16) [] [3] => -1 [4] => 16 )
If i print_r $dbh i get just PDO()
Whats wrong in PDO insert.
Sounds strange and playing with me for a while.
But the same pdo object i use in the below script for select query, i get the 'It is working' message!!
$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();
}