1

I know am attempting to use this syntax to connect from Joomla to SQL Server. However, this throws an ERROR 500 - what is the proper synatx to connect from Joomla To 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());
}

EDIT
I also tried this syntax and also got a 500 error -->

    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();

EDIT
Below are the var_dump($db) results

    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

1 Answers1

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();

In this block you should change this line:

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

to

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

SQLSRV is only SQL Server driver which work with Joomla, if you want use JDatabase.

Furthermore you should check your PHP config, maybe the SQL driver isn't installed.

Oh.. one more. Here:

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

I'm waiting for feedback, give me info if it's works.

Elin
  • 6,507
  • 3
  • 25
  • 47