0

Im trying to connect php to SQL server driver using below:

It works fine for MYSQL., but not for 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;
?>

UPDATE: Have insatalled the pdo_dblib extension.

Im getting

Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /var/www/html/RADIOLOCOUS/GMR/gmrsample_copy.php:14 Stack trace: #0 PDO->__construct('odbc:Driver={SQ...', 'siddharth', '1234') #1 {main} thrown in ....line 14

Any alternate way to connect apart from pdo

Im using Ubuntu 14.04 LAMP with php 5.5

My php_info says:

PDO drivers dblib, mysql

Sach
  • 83
  • 4
  • 24
  • 1
    I hope for your sake the username and password are false. – Option Mar 17 '17 at 15:37
  • 2
    Possible duplicate of [PDO returning error "could not find driver" with a known working DSN](http://stackoverflow.com/questions/31813574/pdo-returning-error-could-not-find-driver-with-a-known-working-dsn) – aynber Mar 17 '17 at 15:40
  • Enable php_pdo_mssql. http://stackoverflow.com/questions/11246007/pdo-mssql-server-driver-not-found – MiltoxBeyond Mar 17 '17 at 15:40
  • as the exception says, you need the driver package installed on the server - another possible dublicate: http://stackoverflow.com/questions/5953882/connecting-to-mssql-using-pdo-through-php-and-linux – cypherabe Mar 17 '17 at 15:43
  • Could you please how to install the same in UBuntu, cause im digging a lot and cannot find it to install in my Ubuntu – Sach Mar 17 '17 at 16:01
  • Does the sql server run several instances? – gus27 Mar 17 '17 at 17:11

2 Answers2

1

The documentation for PDO_DBLIB shows the following DSNs:

mssql:host=localhost;dbname=testdb
dblib:host=localhost;dbname=testdb

So I'd like to suggest this:

$dbh = new PDO("dblib:host=$hostname:1433;dbname=AirportFootfall", $username, $password);

You can test your connection this way:

<?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();
}    
?>
gus27
  • 2,616
  • 1
  • 21
  • 25
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/138403/discussion-on-answer-by-gus27-php-unable-to-connect-to-sql-server-using-pdo-exc). – Bhargav Rao Mar 18 '17 at 15:48
0

It looks like the $mssqldriver var is commented... Did you try to uncomment?? If you look carefully the Connection statement use that var:

$dbh =  new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=AirportFootfall", $username, $password);
uTombou
  • 41
  • 1
  • 6