简体   繁体   English

从托管的Linux PHP访问远程MSSQL Server

[英]Accessing Remote MSSQL Server from hosted linux PHP

I've seen a number of questions asking similar but these are usually answered by having access to the server to install extra packages such as FreeTDS. 我已经看到许多类似的问题,但是通常可以通过访问服务器来安装额外的软件包(例如FreeTDS)来回答这些问题。 We don't have such access as out linux server is hosted with 1and1. 我们没有这种访问权限,因为linux服务器由1and1托管。

My code (from an earlier question by someone else on here) is: 我的代码(来自其他人在此之前提出的问题)是:

  try {
    $hostname = "xx.xx.xx.xx";
    $port = xxxxx;
    $dbname = "ClientDatabase";
    $username = "uuuu";
    $pw = "pppp";
    $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
  $stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
  unset($dbh); unset($stmt);

The error message I get is: 我收到的错误消息是:

Failed to get DB handle: could not find driver 

The MSSQL server is accessible so I can add features to that if necessary. MSSQL服务器是可访问的,因此如有必要,我可以为其添加功能。 The MSSQL server is also running IIS7 but doesn't run PHP. MSSQL服务器也正在运行IIS7,但没有运行PHP。 I know very little about IIS7 but would it be easier to run the PHP scripts on there rather than the hosted linux box? 我对IIS7知之甚少,但是在该处而不是在托管的Linux机器上运行PHP脚本会更容易吗?

Could anyone advise if I can actually connect to the MSSQL server in anyway? 任何人都可以建议我是否真的可以以任何方式连接到MSSQL服务器吗?

Thanks in advance, 提前致谢,

Dave 戴夫

Have you tried mssql_connect? 您是否尝试过mssql_connect?

Here's the syntax: mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] ) 语法如下:mssql_connect([字符串$ servername [,字符串$ username [,字符串$ password [,bool $ new_link = false]]]])

In your case it would be: 您的情况是:

$link = mssql_connect($hostname, $username, $pw);
mssql_select_db($dbname, $link);

Note that $hostname should contain MSSQL Instance Name. 注意$ hostname应该包含MSSQL实例名称。 eg 'KALLESPC\\SQLEXPRESS' 例如'KALLESPC \\ SQLEXPRESS'

After much trial and error I'm in. 经过反复尝试,我进入了。

  try {
    $hostname = "localhost";
    $port = 123456;
    $dbname = "ClientDatabase";
    $username = "uuuu";
    $pw = "pppp";
    $dbh = new PDO ("sqlsrv:Server=$hostname,$port;Database=$dbname","$username","$pw");
  } catch (PDOException $e) {
    echo "Failed to get DB handle: " . $e->getMessage() . "\n";
    exit;
  }
  $stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
  unset($dbh); unset($stmt);

Thanks to those who answered and those who answered similar questions by others. 感谢那些回答了这些问题的人以及那些回答了其他类似问题的人。 Got there.... eventually. 到达那里。

Cheers 干杯

Dave 戴夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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