简体   繁体   中英

Issues with connecting Ubuntu 16.04 to MSSQL

I am trying to connect up to a MSSQL database with Ubuntu16.04 (LAMP stack) server and PHP 7.0

So far I can connect however when I attempt to use the following demo script:

<?php
$conn = mssql_connect("BWSQL", "<usrname>", "<password>");
mssql_select_db( "infobase", $conn );
$query_result = mssql_query( "SELECT * FROM dbo.Staff", $conn );
echo "The field number one is: ";
echo mssql_result ($query_result, 0, 0);
mssql_close($conn); // close connection
?>

I receive Fatal error: Uncaught Error: Call to undefined function mssql_connect() in /var/www/html/test2.php:2 Stack trace: #0 {main} thrown in /var/www/html/test2.php on line 2

I have tested with

php -v

and can see no errors. Command line tests

tsql -S BWSQL -U <usrname> -P <password> -D myData

result in connected and I can call tables up.

I am trying to use sqlsrv/pdo_sqlsrv modules. The freetds module, anything I can get my hands on at this point because this is a test server. I have even tried wrapping everything in html tags. If there is anymore info you require from me please let me know.

Thanks in advance.

The mssql_* family of functions have been deprecated for a very long time, and were finally removed in PHP 7.0 .

You can switch to using PDO as suggested in another answer, or use the sqlsrv_* functions:

<?php
$params = [
    "UID" => "<usrname>",
    "PWD" => "<password>",
    "Database" => "infobase",
];
$conn = sqlsrv_connect("BWSQL", $params);
$query_result = sqlsrv_query($conn, "SELECT * FROM dbo.Staff");
$row = sqlsrv_fetch_array($query_result);
echo "The field number one is: $row[0]";
sqlsrv_close($conn); // close connection

Try using PDO instead of mssql :

$host = "BWSQL"; //azure
$db = "infobase";
$user "<username>";
$pass = "<password>";    
try {
    $pdo = new PDO("sqlsrv:server=$host;Database=$db", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} 
catch (PDOException $e) 
{
    print("Error connecting to SQL Server.");
    die(print_r($e));
}
$sql = "SELECT * FROM dbo.Staff";
$qry = $pdo->prepare($sql);
$qry->execute();
$result = $qry->fetchColumn(0);
echo "The field number one is: $result";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM