install ms sql pdo for windows in php

To install Microsoft SQL Server PDO driver on Windows, follow these steps:

  1. Download the latest version of the Microsoft Drivers for PHP for SQL Server from here.

  2. Extract the downloaded file and copy the php_sqlsrv_XXX and php_pdo_sqlsrv_XXX folders to your PHP ext/ directory.

  3. Open your php.ini file and add the following lines to enable the extensions:

    main.php
    extension=php_sqlsrv_XXX.dll
    extension=php_pdo_sqlsrv_XXX.dll
    
    62 chars
    3 lines

    Replace the XXX with the appropriate version number.

  4. Save and close php.ini file.

  5. Restart your web server.

  6. To verify that the drivers are successfully installed, create a new PHP file with the following code:

    main.php
    <?php
    $server = "your_server_name";
    $database = "your_database_name";
    $username = "your_username";
    $password = "your_password";
    
    try {
        $conn = new PDO("sqlsrv:server=$server;database=$database", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
    }
    catch (PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
    ?>
    
    409 chars
    16 lines

    Replace the your_server_name, your_database_name, your_username and your_password with your own information.

    If the connection is successful, you should see a message saying "Connected successfully".

related categories

gistlibby LogSnag