连接到 Joomla 上的 SQL 服务器

Connect To SQL Server On Joomla

我知道我正在尝试使用此语法从 Joomla 连接到 SQL 服务器。但是,这会引发 ERROR 500 - 从 Joomla 连接到 SQL 服务器的正确语法是什么?

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $uid = "user";
    $pwd = "pwd";
    $DB = "database";
    $serverName = "IP";

$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database"=> $DB, "ReturnDatesAsStrings" => true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

编辑
我也试过这个语法,也得到了 500 错误 -->

    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$option = array(); 
$option['driver'] = 'mssql'; 
$option['host'] = 'server'; 
$option['user'] = 'user'; 
$option['password'] = 'pass'; 
$option['database'] = 'db'; 
$option['prefix'] = ''; 
$db = JDatabase::getInstance( $option );
$query = $db->getQuery(true);
$query = "SELECT * from Test";
$db->setQuery($query); 
$results = $db->loadObjectList();

编辑
以下是 var_dump($db) 结果

    object(JDatabaseDriverMssql)[1788]
  public 'name' => string 'mssql' (length=5)
  protected 'nameQuote' => null
  protected 'nullDate' => string '1900-01-01 00:00:00' (length=19)
  private '_database' (JDatabaseDriver) => string 'db' (length=9)
  public 'serverType' => null
  protected 'connection' => null
  protected 'count' => int 0
  protected 'cursor' => null
  protected 'debug' => boolean false
  protected 'limit' => int 0
  protected 'log' => 
    array (size=0)
      empty
  protected 'timings' => 
    array (size=0)
      empty
  protected 'callStacks' => 
    array (size=0)
      empty
  protected 'offset' => int 0
  protected 'options' => 
    array (size=7)
      'driver' => string 'mssql' (length=5)
      'host' => string 'server' (length=25)
      'user' => string 'user' (length=7)
      'password' => string 'pass' (length=9)
      'database' => string 'db' (length=9)
      'prefix' => string '' (length=0)
      'select' => boolean true
  protected 'sql' => null
  protected 'tablePrefix' => string '' (length=0)
  protected 'utf' => boolean true
  protected 'utf8mb4' => boolean false
  protected 'errorNum' => int 0
  protected 'errorMsg' => null
  protected 'transactionDepth' => int 0
  protected 'disconnectHandlers' => 
    array (size=0)
      empty
    $option = array(); 
    $option['driver'] = 'mssql'; 
    $option['host'] = 'server'; 
    $option['user'] = 'user'; 
    $option['password'] = 'pass'; 
    $option['database'] = 'db'; 
    $option['prefix'] = ''; 
    $db = JDatabase::getInstance( $option );
    $query = $db->getQuery(true);
    $query = "SELECT * from Test";
    $db->setQuery($query); 
    $results = $db->loadObjectList();

在此块中,您应该更改此行:

$option['driver'] = 'mssql'; 

$option['driver'] = 'sqlsrv';

SQLSRV 只是 SQL 与 Joomla 一起工作的服务器驱动程序,如果你想使用 JDatabase。

此外,您应该检查您的 PHP 配置,也许 SQL 驱动程序未安装。

哦..再来一个。这里:

$db = &JDatabase::getInstance($option);

我正在等待反馈,如果有效请给我信息。