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

6

2 Answers 2

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();
}    
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
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);

3 Comments

Alse, please make sure you have installed "php5-sybase"
Have updated my answer @uTombou, have spending much time on this, really need to fix this...
Any other alternate to this fix, have updated my question with code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.