我试图使用下面的方法将php连接到SQL服务器驱动程序:
它适用于MYSQL。但不适用于SQL Server。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$data = json_decode(file_get_contents('php://input'), true);
if(!empty($data)):
header('Content-Type:text/plain');
$hostname = '10.8.8.9';
$username = 'siddharth';
$password = '1234';
$dbname = 'AirportFootfall';
$mssqldriver = '{SQL Server}';
//$dbh = new PDO("mssql:host=$hostname;dbname=AirportFootfall", $username, $password);
//$dbh = new PDO("sqlsrv:Server=10.16.34.90;Database=AirportFootfall", $username, $password);
//$dbh = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=AirportFootfall", $username, $password);
//$dbh = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=AirportFootfall", $username, $password);
//$dbh = new PDO("dblib:host=$hostname;dbname=AirportFootfall", $username, $password);
//$dbh = new PDO("dblib:host=$hostname;dbname=$dbname", $username, $password);
$dbh = new PDO("dblib:host=$hostname;dbname=$dbname", $username, $password);
$arraykey=array_keys($data);
$array=$data[$arraykey[0]];
try
{
$count = $dbh->exec('INSERT INTO RadioCon_Sensor_Raw_Data(version,visitorId,dwellTime,poiId,srId,zoneId,poiProximityConfidence,zoneProximityConfidence,poiPresenceConfidence,zonePresenceConfidence,normalizedTime) VALUES ("' . implode('", "', $array) . '")' ) or die(print_r($dbh->errorInfo(), true));
//echo $count;
$dbh = null;
echo 'Data Successfully inserted!!<br />';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
endif;
?>
更新:已经破解了pdo_dblib扩展名。
我得到了
致命错误:未捕获的异常&#39; PDOException&#39;有消息&#39;无法找到驱动程序&#39;在/var/www/html/RADIOLOCOUS/GMR/gmrsample_copy.php:14堆栈跟踪:#0 PDO-&gt; __ construct(&#39; odbc:Driver = {SQ ...&#39;,&#39; siddharth&#39;,&#39; 1234&#39;)#1 {main}投入....第14行
除pdo之外的任何其他连接方式
我使用Ubuntu 14.04 LAMP和php 5.5
我的php_info说:
PDO驱动程序dblib,mysql
答案 0 :(得分:1)
PDO_DBLIB的文档显示了以下DSN:
mssql:host=localhost;dbname=testdb
dblib:host=localhost;dbname=testdb
所以我想建议一下:
$dbh = new PDO("dblib:host=$hostname:1433;dbname=AirportFootfall", $username, $password);
您可以通过以下方式测试您的连接:
<?php
header('Content-Type:text/plain');
$hostname = '10.8.8.9';
$username = 'siddharth';
$password = '1234';
$dbname = 'AirportFootfall';
try {
$dbh = new PDO("dblib:host=$hostname:1433;dbname=$dbname", $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)
看起来$ mssqldriver var被评论了...你试图取消注释吗?如果仔细查看Connection语句,请使用该var:
$dbh = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=AirportFootfall", $username, $password);