【问题标题】:Connection to remote MS SQL database from Linux server using PHP使用 PHP 从 Linux 服务器连接到远程 MS SQL 数据库
【发布时间】:2017-12-30 13:08:34
【问题描述】:

我在 Linux 服务器上托管了我的网站,我想连接 MS SQL 数据库。我在编程中使用过 PHP。我已经联系了这两家服务器提供商,他们在一定程度上提供了帮助。但是我的问题没有解决,你能指导我做什么吗?我的代码如下。

当我运行它时,它显示“找不到驱动程序1”

请指导我。提前致谢

<?php 

//echo phpinfo();

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>testing</h1>
</body>
</html>

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = 'server:port';
$myDB = "DatabaseName";

// Connect to MSSQL
$link = mssql_connect($server, 'username', 'password');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
else
{
 echo "success";
}
?>

【问题讨论】:

    标签: php sql-server linux database database-connection


    【解决方案1】:
    <?php
    
    try {
    
        $conn = new PDO("sqlsrv:Server='server_name';Database=database_name", 'username', 'password');
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    } catch (Exception $e) {
    
        die(print_r($e->getMessage() ));
    }
    
    $tsql = "select * from table_name";
    $getResults = $conn->prepare($tsql);
    $getResults->execute();
    $results = $getResults->fetchAll(PDO::FETCH_BOTH);
    
    foreach ($results as $row) {
      echo $row['0'].' '.$row['6'];
      echo "<br>";
    }
    
    
    ?>
    

    【讨论】:

    • 感谢@Rony 的帮助
    猜你喜欢
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-17
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多