[英]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 我正在尝试使用Ubuntu16.04(LAMP stack)服务器和PHP 7.0连接到MSSQL数据库
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 我收到致命错误:未捕获错误:调用/var/www/html/test2.php中未定义的函数mssql_connect():2堆栈跟踪:#0 {main}在线中/var/www/html/test2.php中引发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. 我正在尝试使用sqlsrv / pdo_sqlsrv模块。 The freetds module, anything I can get my hands on at this point because this is a test server.
freetds模块,这是我可以尝试的所有内容,因为这是一个测试服务器。 I have even tried wrapping everything in html tags.
我什至尝试将所有内容包装在html标签中。 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 . mssql_*
系列函数已被弃用很长时间, 并最终在PHP 7.0中删除 。
You can switch to using PDO as suggested in another answer, or use the sqlsrv_*
functions: 您可以按照另一个答案中的建议切换到使用PDO ,或使用
sqlsrv_*
函数:
<?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
: 尝试使用PDO而不是
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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.