Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法通过PHP连接到msSQL数据库_Php_Sql_Sql Server_Sql Server 2005_Connection - Fatal编程技术网

无法通过PHP连接到msSQL数据库

无法通过PHP连接到msSQL数据库,php,sql,sql-server,sql-server-2005,connection,Php,Sql,Sql Server,Sql Server 2005,Connection,我正在使用当前代码尝试访问msSQL 2005数据库: <?php $myServer = "[server]"; $myUser = "[username]"; $myPass = "[password]"; $myDB = "[db]"; //connection to the database $dbhandle = mssql_connect($myServer, $myUser, $myPass) or die("Couldn't connect to SQL Server

我正在使用当前代码尝试访问msSQL 2005数据库:

<?php
$myServer = "[server]";
$myUser = "[username]";
$myPass = "[password]";
$myDB = "[db]";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT id, name, year ";
$query .= "FROM cars ";
$query .= "WHERE name='BMW'";

//execute the SQL query and return records
$result = mssql_query($query);

$numRows = mssql_num_rows($result);
echo "<h1>" . $numRows . " Row" . ($numRows == 1 ? "" : "s") . " Returned </h1>";

//display the results
while($row = mssql_fetch_array($result))
{
  echo "<li>" . $row["id"] . $row["name"] . $row["year"] . "</li>";
}
//close the connection
mssql_close($dbhandle);
?>

您认为问题出在哪里?

凭据无效,如果您不使用本地主机,请确保将服务器的ip添加到安全列表中

尝试调用以获取最后一条错误消息:

$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer. Error: " . mssql_get_last_message());

我觉得你的一个DLL版本不对。从SQL2000迁移到SQL2005时出现了某种问题,PHP的创建者没有自己解决。这里有很多关于它的帖子:

我相信DLL是ntwdblib.DLL,版本至少需要是2000.80.194.0版。如果您运行的是Apache或WampServer,则有一个存储Apache dll的相同dll需要覆盖

注意:几天前我遇到了这个问题,找到了正确的DLL并覆盖了这两个DLL,使它能够工作


另外:您可能需要设置远程连接。默认情况下,Sql Server 2005已禁用远程连接。您可以通过运行SQL Surface Area Configuration utility来允许远程连接。

几个月前,我在这方面遇到了一些困难,我发现唯一可行的方法是在指定服务器时包含实例名称。例如:

$myServer = "SERVER\INSTANCENAME";

即使启用了TCP/IP,仅指定服务器也无法工作。

首先尝试在浏览器上执行类似于
phpinfo()
的操作,然后查看允许使用哪些数据库

如果您没有看到类似于
mssql
的内容,则未对其进行配置。因此,请使用将配置的
odbc\u connect()
。您的连接字符串如下所示:

$server = '';
$user = '';
$password = '';
$database = '';
$connection = odbc_connect("Driver={SQL Server Native Client `11.0};Server=$server;Database=$database;", $user, $password);`
如果您使用的是mssql 2005或2008,则将d 11.0更改为10.0,对于其他版本,只需更改为mssql的版本。

停止使用

mssql_连接

然后开始使用

sqlsrv_连接

那会帮你省去很多麻烦。此外,函数*mssql_connect*已被弃用

要使用sqlsrv_connect,必须下载驱动程序并将其安装为PHP的扩展,以识别sqlsrv函数。从Microsoft下载中心下载驱动程序(搜索“sql server php驱动程序”),在撰写本文时,下载url为:

正确的安装步骤由Microsoft自己在

  • 下载驱动程序。2.把它放在PHPEXT文件夹中。3.更新php.ini文件4。重新启动服务器
  • 安装sql server驱动程序后,只需按照中的说明进行操作。下面是一个快速片段:

    <?php
    $serverName = "serverName\sqlexpress"; //serverName\instanceName
    
    // Since UID and PWD are not specified in the $connectionInfo array,
    // The connection will be attempted using Windows Authentication.
    $connectionInfo = array( "Database"=>"dbName");
    $conn = sqlsrv_connect( $serverName, $connectionInfo);
    
    if( $conn ) {
         echo "Connection established.<br />";
    }else{
         echo "Connection could not be established.<br />";
         die( print_r( sqlsrv_errors(), true));
    }
    ?>
    
    
    

    投票

    在此站点下载驱动程序,然后打开php.ini文件并添加下载的DLL。

    响应延迟,但可能会对某些人有所帮助。 我们快到了,这是连接参数的问题, 可能需要将servername命名为
    IP:port

    服务器名::MS SQL server。e、 g.
    hostname:port(Linux)或hostname,port(Windows)。

    服务器名称应为“服务器\实例” 实例只不过是与1433不同的特定端口地址。。。因此,只需在mssql服务器上查找端口,然后尝试使用:
    ip:port

    对我来说完全适用的示例代码::

        ini_set('display_errors', '1');
        // $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
        $servername = "123.21.47.23:22320";
        $myUser = "UserName";
        $myPass = 'xxxxxxxx';
        $myDB = "[database]"; 
    
        //connection to the database
        $dbhandle = mssql_connect($servername, $myUser, $myPass)
            or die("Couldn'tt connect to SQL Server on $myServer"); 
        if($dbhandle) {
         echo "Success, Connected\r\n";
        } else {
            echo "problem :( \r\n";
        }
    
    希望这能帮助一些人:)
    快乐编码

    如果您使用windows 10

    第一步。单击开始按钮

    第二步。键入服务并输入(打开应用程序服务)

    第三步。查找-SQL Server(MSSQLSERVER)

    第四步。右键单击->选择属性

    第五步。显示弹出窗口。选择第二个选项卡(登录)

    第六步。选择本地系统帐户

    第七步。并选中“允许服务与桌面交互”

    第八步。最后单击ok

    1次刷新 现在已连接。
    我希望它非常有用

    我支持该动议,您可以查看以下链接:mssql_get_last_message()获取sql server发送的最后一条消息。如果你无法连接。。。没有消息。这对错过的连接没有帮助。我们如何找到诸如“密码错误”、“未知主机”或“未知用户”之类的连接错误消息?事实上,我发现这对于连接尝试来说是相当标准的。那么,当mssql_get_last_message()在连接尝试中没有返回任何内容时,这意味着什么呢?dubugger在遇到
    sqlsrv_connect($serverName,$connectionInfo)
    时崩溃。我还发布了一个与此相关的查询,崩溃的原因可能是什么?mysql_connect()已被弃用,而不是mssql_connect()mssql_connect()并非不推荐我必须为win 2012 r2服务器安装x64。
        ini_set('display_errors', '1');
        // $myServer = "winsrv22.somedns.co.uk:22320";//this style works as well
        $servername = "123.21.47.23:22320";
        $myUser = "UserName";
        $myPass = 'xxxxxxxx';
        $myDB = "[database]"; 
    
        //connection to the database
        $dbhandle = mssql_connect($servername, $myUser, $myPass)
            or die("Couldn'tt connect to SQL Server on $myServer"); 
        if($dbhandle) {
         echo "Success, Connected\r\n";
        } else {
            echo "problem :( \r\n";
        }