SQLSERV和PHP不工作。。。无法创建到数据库的连接
我已经经历了,据我所知,它是有效的。(SQLSERV和PHP不工作。。。无法创建到数据库的连接,php,sql,sql-server,Php,Sql,Sql Server,我已经经历了,据我所知,它是有效的。(phpinfo()在添加的扩展列表中显示它) 我有我从MSDN得到的代码。我对它做了一些修改,以便它可以与我的SQL Server和数据库一起工作。数据库就在那里,并且正在运行 这是我的密码: <?php ini_set('display_startup_errors', 1); ini_set('display_errors', 1); error_reporting(-1); ?> 所以它是固定的。。 问题是$se
phpinfo()
在添加的扩展列表中显示它)
我有我从MSDN得到的代码。我对它做了一些修改,以便它可以与我的SQL Server和数据库一起工作。数据库就在那里,并且正在运行
这是我的密码:
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
?>
所以它是固定的。。
问题是$serverName=“(本地)”代码>给出了问题
我所要做的就是,$serverName=“localhost\MSSQL2014”代码>
谢谢大家。您是想使用sqlsrv
还是PDO
?您的标题建议使用前者,但示例建议后者尝试转储实际的错误代码和消息,而不仅仅是die(“连接到SQL Server的错误”)
您是否尝试过$dbh=newpdo('mysql:host=localhost;dbname=test',$user,$pass)代码>这个,当然是用你的凭证替换的例子。@RiggsFolly,我现在就这么做了。。随后出现更多描述性错误消息。我现在无法登录用户sa。(尝试sa而不是普通用户)@Fred ii-我正在使用MSSQL 2014。没有MySQL。
<?php
$serverName = "(local)";
$database = "{db}";
// Get UID and PWD from application-specific files.
$uid = "{user}";
$pwd = "{pass}";
try {
$conn = new PDO( "sqlsrv:server=$serverName;Database = $database", $uid, $pwd);
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch( PDOException $e ) {
die( "Error connecting to SQL Server" );
}
echo "Connected to SQL Server\n";
$query = 'select * from {table}';
$stmt = $conn->query( $query );
while ( $row = $stmt->fetch( PDO::FETCH_ASSOC ) ){
print_r( $row );
}
// Free statement and connection resources.
$stmt = null;
$conn = null;
?>
PDOException Object ( [message:protected] => SQLSTATE[28000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'sa'. [string:Exception:private] => [code:protected] => 28000 [file:protected] => C:\inetpub\wwwroot\sandbox\sfactor\sandbox.php [line:protected] => 20 [trace:Exception:private] => Array ( [0] => Array ( [file] => C:\inetpub\wwwroot\sandbox\sfactor\sandbox.php [line] => 20 [function] => __construct [class] => PDO [type] => -> [args] => Array ( [0] => sqlsrv:server=;Database=SFactor [1] => sa [2] => 7536SqL ) ) ) [previous:Exception:private] => [errorInfo] => Array ( [0] => 28000 [1] => 18456 [2] => [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'sa'. ) ) Connected to SQL Server Notice: Undefined variable: conn in C:\inetpub\wwwroot\sandbox\sfactor\sandbox.php on line 31 Fatal error: Call to a member function query() on a non-object in C:\inetpub\wwwroot\sandbox\sfactor\sandbox.php on line 31