ติดตั้ง php sqlsrv

1. ตรงสอบ version ของ php
    $php -v
    PHP 7.4.3-4ubuntu2.19 (cli) (built: Jun 27 2023 15:49:59) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
        with Zend OPcache v7.4.3-4ubuntu2.19, Copyright (c), by Zend Technologies|
2. ติดตั้ง sqlsrv และ pdo_sqlsrv
    php8
        $sudo pecl install pdo_sqlsrv
        $sudo pecl install sqlsrv
    php7
        sudo pecl install sqlsrv-5.11.0beta1
        sudo pecl install pdo_sqlsrv-5.11.0beta1
3. สร้างแฟ้ม /etc/php/x/mods-available/sqlsrv.ini
    มีรายละเอยีดดังนี้
    extension=sqlsrv.so
4. สร้างแฟ้ม /etc/php/x/mods-available/pdo_sqlsrv.ini
    มีรายละเอยีดดังนี้
    extension=pdo_sqlsrv.so
5. ติดตั้ง obdc driver
    $curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc
    $curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
    $sudo apt-get update
    $sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
    $sudo ACCEPT_EULA=Y apt-get install -y mssql-tools18
    $echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
    $source ~/.bashrc
    $sudo apt-get install -y unixodbc-dev
    $sudo service apache2 restart
6. restart server apache2
    sudo service apache2 restart
7. ทดสอบ สร้าง test_mssql.php

<?php
$host= "localhost";
$user="test_user";
$pass="test_password";
$dbname="test_db";

try{
    $conn = new PDO("sqlsrv:server=$host ;Database=$dbname;TrustServerCertificate=true;", "$user", "$pass");
}catch(PDOException $e){
    echo ($e->getMessage());        
}    

$sql = "SELECT * FROM test_table";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo"1";

while($result = $stmt->fetch( PDO::FETCH_ASSOC )){
    echo $result["filed1"]."<br>";
}

$conn = null;   
?>

 

Rating

Average: 1 (1 vote)