$-6ren">
gpt4 book ai didi

php - 通过 sqlsrv_connect() 连接到 mssql 数据库 - PHP

转载 作者:可可西里 更新时间:2023-11-01 01:00:52 25 4
gpt4 key购买 nike

我正在尝试使用 sqlsrv_connect() 函数使用 PHP 连接到 2005 Microsoft sql 数据库。唉,函数返回 false,我不确定为什么。

<?php
$myServer = "MAZE.jpc.wa.edu.au.local";
$myUser = "myUsername";
$myPass = "myPassword";
$myDB = "John Paul College";
$connectionInfo = array("Database"=>$myDB, "UID" => $myUser, "PWD" => $myPass);

$conn = sqlsrv_connect($myServer, $connectionInfo); //returns false
if( $conn === false )
{
echo "failed connection";
}

$sql = "SELECT name FROM users WHERE name= 'admin'";
$stmt = sqlsrv_query($conn,$sql);
if(sqlsrv_fetch($stmt) ===false)
{
echo "couldn't fetch data";
}
$name = sqlsrv_get_field($stmt,0);
echo $name;
sqlsrv_close( $conn );
?>

有谁知道为什么我无法连接?谢谢。

编辑。

好的,好吧,多亏了其他人的回答,我才能够提出错误消息,其中指出

Array (
[0] => Array (
[0] => IMSSP [SQLSTATE] => IMSSP
[1] => -49 [code] => -49
[2] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later)
or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server.
Neither of those ODBC Drivers are currently installed.
Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver
for x86: http://go.microsoft.com/fwlink/?LinkId=163712
[message] => This extension requires either the Microsoft SQL Server 2008 Native Client (SP1 or later)
or the Microsoft SQL Server 2008 R2 Native Client ODBC Driver to communicate with SQL Server.
Neither of those ODBC Drivers are currently installed.
Access the following URL to download the Microsoft SQL Server 2008 R2 Native Client ODBC driver
for x86: http://go.microsoft.com/fwlink/?LinkId=163712 )
[1] => Array (
[0] => IM002 [SQLSTATE] => IM002
[1] => 0 [code] => 0
[2] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
[message] => [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified ) )

我不完全确定如何着手解决这个问题。我使用 XAMPP 作为我的测试网络服务器,PHP 5.3 版和以下 .dllphp_sqlsrv_53_ts_vc6.dllphp_pdo_sqlsrv_53_ts_vc6.dll

最佳答案

我建议您使用 sqlsrv_errors() 显示连接错误。请看下面的实现。

<?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));
}
?>

更多信息:http://php.net/manual/en/function.sqlsrv-connect.php

关于php - 通过 sqlsrv_connect() 连接到 mssql 数据库 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27308120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com