0

I am having issues in installing MSSQL in my php library.

I am getting below error

Fatal error: Uncaught PDOException: could not find driver in C:\xampp\htdocs\img\test.php:7 Stack trace: #0 C:\xampp\htdocs\img\test.php(7): PDO->__construct('sqlsrv:server=D...', '', '') #1 {main} thrown in C:\xampp\htdocs\img\test.php on line 7

I have added below dll files as per the R&D but still I am not able to run mssql. I am working with my local system and installed SQL server 2012 and MS SQL manager I have created a database name THL with sone table I added server name (local)\sqlexpress but it did'nt worked so I changed it to DESKTOP-DDOS0G1\sqlexpress still it is not working.

enter image description here

My connection code is below

<?php
$serverName = 'DESKTOP-DDOS0G1\sqlexpress';
$conn = new PDO("sqlsrv:server=$serverName ; Database=DHL", "", "");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  
if(!$conn) {
    echo 'Something went wrong while connecting to MSSQL';
    die(print_r(sqlsrv_errors(),true));
}else
{
    echo 'connected';
}
?>

Please suggest what things I have missed in this process. I have also installed ODBC.

1
  • See updated answer. It is possible to mix PDO and not PDO approach. Commented Nov 22, 2018 at 8:27

1 Answer 1

3

Explanations:

You need to install or configure PHP Driver for SQL Server (sqlsrv and/or pdo_sqlsrv PHP extension).

  • Check Microsoft PHP Drivers for SQL Server Support Matrix and download appropriate version of this driver. If you use PHP 7 or later, you should choose version 4.0, 4.3, 5.2 or 5.3 (32-bit or 64-bit also depends on PHP version).

  • Check System Requirements for the Microsoft Drivers for PHP for SQL Server. An appropriate ODBC driver installation is also required.

  • Load PHP Driver for SQL Server as PHP extension.

Check the configuration with <?php phpinfo();?>. There should be a section with name pdo_sqlsrv (if you use PDO) and/or sqlsrv (without PDO).

Examples:

With PDO:

<?php
$serverName = 'DESKTOP-DDOS0G1\sqlexpress';
try
    $conn = new PDO("sqlsrv:server=$serverName ; Database=DHL", "", "");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
} catch(PDOException $e) {
    die("Error connecting to SQL Server".$e->getMessage());
}   
?>

Without PDO:

<?php
$serverName = 'DESKTOP-DDOS0G1\sqlexpress';
$cinfo = array(
    "Database" => "DHL"
);
$conn = sqlsrv_connect($serverName, $cinfo);
if ($conn === false) {
    echo "Error (sqlsrv_connect): ".print_r(sqlsrv_errors(), true);
    exit;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

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.