如何将 PHP 连接到 SQL Server?

Posted

技术标签:

【中文标题】如何将 PHP 连接到 SQL Server?【英文标题】:How to connect PHP to SQL Server? 【发布时间】:2012-04-19 12:58:52 【问题描述】:

我正在尝试使用 php 连接到 SQL Server,我正在学习这种语言。

我已经读到可能 ODBC 连接是最好的,我已经创建了我的 ODBC,但我不知道如何从 PHP 中使用它,到目前为止我正在尝试这个:

$server = 'MyServer\MyDB';

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

if (!$link) 
    die('You cannot connect to MSSQL');

运行此程序时,我收到以下消息:

找不到对象,错误 404。

但是 ODBC 测试没问题。

SQL Server 位于同一台PC,所以也许我不需要输入IP。

有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

看来您可能有错字。以下是来自the documentation 的示例:

// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');

if (!$link) 
    die('Something went wrong while connecting to MSSQL');

您错误地将= 放在mssql_connect 之后,试图进行分配(就解释器而言)。

至于使用 MSSql 和 PHP,看看相关的mssql functions。接下来你会select your database,然后开始querying it。

【讨论】:

不应该是“就解释器而言”而不是编译器吗?

以上是关于如何将 PHP 连接到 SQL Server?的主要内容,如果未能解决你的问题,请参考以下文章