1

我知道正在尝试使用此语法从 Joomla 连接到 SQL Server。但是,这会引发一个ERROR 500问题——从 Joomla 连接到 SQL Server 的正确语法是什么?

    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
4

1 回答 1

0
    $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';

如果您想使用 JDatabase,SQLSRV 只是与 Joomla 一起使用的 SQL Server 驱动程序。

此外,您应该检查您的 PHP 配置,可能没有安装 SQL 驱动程序。

哦。。再来一张。这里:

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

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

于 2017-03-17T01:05:52.390 回答