【问题标题】:Connect To SQL Server On Joomla在 Joomla 上连接到 SQL Server
【发布时间】:2017-08-04 04:32:36
【问题描述】:

我知道我正在尝试使用此语法从 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

【问题讨论】:

  • $db = JDatabase::getInstance( $option ); 之后执行var_dump($db); exit; 在此处发布结果
  • @AmitRay - 编辑帖子以显示结果。

标签: php sql-server joomla joomla3.5


【解决方案1】:
    $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);

我正在等待反馈,如果可行,请给我信息。

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多