-1

i try to query MSSQL Server. The query is very simple but still get only errors. The version of PHP is 7.0.28 and code is here:

$serverName = "(local)";
/* Connect using Windows Authentication. */
try
{
    $conn = new PDO("sqlsrv:server=$serverName ; Database=mgm59ood", "sa", "********");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    if ($conn)
    {
        $tsql = "select name from goods";
        $getResults = sqlsrv_query($conn, $tsql);

        // Error handling

        if ($getResults == FALSE)
        {
            die(FormatErrors(sqlsrv_errors()));
        }
    }
}

catch(Exception $ex)
{
    die(print_r($ex->getMessage()));
}

In my browser throw exception in line with $getResults.

Fatal error: Uncaught Error: Call to undefined function sqlsrv_query() in C:\xampp\htdocs\testwithsql\index.php:26 Stack trace: #0 {main} thrown in C:\xampp\htdocs\testwithsql\index.php on line 26

Please help me.

spootles
  • 73
  • 9

1 Answers1

2

Did you install any driver for MS SQL?

If you didn't do it then:

Demo with PDO and without PDO:

<?php

echo "Test with php_pdo_sqlsrv (PDO)\n";

$serverName = "(local)";
try
{
    $dbh = new PDO("sqlsrv:server=$serverName; Database=Test", "sa", "...");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $tsql = "select 1 value";
    $result = $dbh->prepare($tsql)->execute();

    echo $result;
}
catch(PDOException $e)
{  
    print_r($e->getMessage());
}


echo "\nTest with php_sqlsrv (not PDO)\n";

$serverName = "(local)";
$connectionInfo = array( "Database"=>"Test", "UID"=>"sa", "PWD"=>"...");
try
{
    $conn = sqlsrv_connect($serverName, $connectionInfo);

    $tsql = "select 2 value";
    $result = sqlsrv_query($conn, $tsql);
    $obj = sqlsrv_fetch_object($result);
    echo $obj->value;   
}
catch(Exception $ex)
{
    print_r($ex->getMessage());
}

?>
Sergey Menshov
  • 3,856
  • 2
  • 8
  • 19
  • I add drivers from the link and same error. I add them before this post and if i had no drivers error will be different. – spootles Apr 10 '18 at 06:43
  • @spootles - Yes, you're right it wasn't a driver error. I've added two examples - with PDO and without PDO. – Sergey Menshov Apr 10 '18 at 07:44