我尝试查询MSSQL Server。查询非常简单,但仍然只能得到错误。 PHP的版本是7.0.28,代码在这里:
$serverName = "(local)";
/* Connect using Windows Authentication. */
try
{
$conn = new PDO("sqlsrv:server=$serverName ; Database=mgm59ood", "sa", "********");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if ($conn)
{
$tsql = "select name from goods";
$getResults = sqlsrv_query($conn, $tsql);
// Error handling
if ($getResults == FALSE)
{
die(FormatErrors(sqlsrv_errors()));
}
}
}
catch(Exception $ex)
{
die(print_r($ex->getMessage()));
}
在我的浏览器中抛出与$ getResults一致的异常。
致命错误:未捕获错误:调用未定义函数sqlsrv_query() 在C:\ xampp \ htdocs \ testwithsql \ index.php:26堆栈跟踪:#0 {main} 在第26行的C:\ xampp \ htdocs \ testwithsql \ index.php中抛出
请帮帮我。
答案 0 :(得分:2)
您是否为MS SQL安装了任何驱动程序?
如果你没有这样做,那么:
extension=php_sqlsrv_xxx.dll
或extension=php_pdo_sqlsrv_xxx.dll
添加到php.ini
(查看SQLSRV_Readme.htm
)。使用PDO进行演示而不使用PDO:
<?php
echo "Test with php_pdo_sqlsrv (PDO)\n";
$serverName = "(local)";
try
{
$dbh = new PDO("sqlsrv:server=$serverName; Database=Test", "sa", "...");
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$tsql = "select 1 value";
$result = $dbh->prepare($tsql)->execute();
echo $result;
}
catch(PDOException $e)
{
print_r($e->getMessage());
}
echo "\nTest with php_sqlsrv (not PDO)\n";
$serverName = "(local)";
$connectionInfo = array( "Database"=>"Test", "UID"=>"sa", "PWD"=>"...");
try
{
$conn = sqlsrv_connect($serverName, $connectionInfo);
$tsql = "select 2 value";
$result = sqlsrv_query($conn, $tsql);
$obj = sqlsrv_fetch_object($result);
echo $obj->value;
}
catch(Exception $ex)
{
print_r($ex->getMessage());
}
?>