Hi,

I am using a site with multiple DB connections. The main (default) connection is to MySQL and that is working fine. I coded an small module in Drupal 7 to connect to MS-SQL server in my office and I am getting this error:

[warn] mod_fcgid: stderr: PHP Fatal error:  Undefined class constant 'SQLSRV_ATTR_DIRECT_QUERY' in /home/XXXXXX/public_html/includes/database/sqlsrv/database.inc on line 60

What am I missing?

Drupal 7
PHP 5.3.5
MSSQL 2000

Comments

Anonymous’s picture

The driver doesn't support SQL Server 2000. Just SQL Server 2005 or later.

emilorol’s picture

Humm... I coded an small script just to make sure I had connection and I was able to connect using the regular PHP function mssql_connect and mssql_query. So PDO MS-SQL doesn't support MS-SQL 2000?

Anonymous’s picture

No, just MSSQL 2005 or greater.

From their page on CodePlex http://sqlsrvphp.codeplex.com/

The SQLSRV extension provides a procedural interface while the PDO_SQLSRV extension implements PDO for accessing data in all editions of SQL Server 2005 and later

ashayc’s picture

Issue tags: -mssql +mssql sqlsrv

Let me clear up some confusion here : MSSQL != SQLSRV

- MSSQL driver (PDO or not) is the community authored driver based on deprecated technologies. It supports SQL 2000 but does not support newer capabilities in SQL 2005 or later.

- SQLSRV driver (PDO or not) is the Microsoft authorized driver based on current supported technologies. It suppots SQL 2005 and later - on Windows. DamZ's patch/module is based on the PDO_SQLSRV driver for WIndows deployments, SQL 2008 or later is recommended.

PDO_SQLSRV defines the SQLSRV_ATTR_DIRECT_QUERY attribute used by DamZ's patch/module, PDO_MSSQL does not (the "SQLSRV_" prefix is a good clue).

emilorol’s picture

@ashayc thank you for the clarification.

OK, so in a nutshell I have a linux server with Drupal 7 setup and since so much advertising has been made to the new PDO support, I was thinking to use it to have an small module connect to the office MS-SQL server grab some data and display it in a page when call. That is the reason why I was using this driver.

Should I use something else?

BTW I was playing with the code and I change it to:

    // Launch the connection to the server.
    parent::__construct('sqlsrv:' . implode(';', $options), $connection_options['username'], $connection_options['password']);
    
/*
    parent::__construct('sqlsrv:' . implode(';', $options), $connection_options['username'], $connection_options['password'], array(
      PDO::SQLSRV_ATTR_DIRECT_QUERY => TRUE,
    )); 
*/ 

Now I am getting a connection, but it seems to be failing at the db_query and also at:

Uncaught exception thrown in shutdown function.

PDOException: could not find driver in db_delete() (line 2352 of /home/XXXX/public_html/includes/database/database.inc).

Once again thank you,
Emil

ashayc’s picture

Emil,

"I have a linux server with Drupal 7 setup and since so much advertising has been made to the new PDO support, I was thinking to use it to have an small module connect to the office MS-SQL server grab some data and display it in a page when call"
I'm afraid I'm not much of help on Linux. I can say this, DamZ designed this module for the PDO_SQLSRV design and, as you have found, I think it's going to take a fair bit of work to change it a little at time with each issue until you get it to work on PDO_MSSQL. It's all on Windows with this driver or nothing, I'm afraid.

PHP support on Windows is much better these days (IIS, SQL Server, and PHP itself), maybe you give it a try with a test install of your site?

emilorol’s picture

Category: support » bug
Status: Closed (won't fix) » Active

Here is my final working solution in case someone need it a simple way to connect to MS-SQL 2000 from Drupal 7 in Linux:

<?php
function module_page(){
	$header = array("No.","A","B","C");
	$rows = array();
	
	// Get some information from a non-Drupal database. 
	$conn = new PDO("dblib:host=192.168.1.100;dbname=YOUR_DB", 'YOUR_USERNAME', 'YOUR_PASSWORD'); 
   	$result = $conn->query("SELECT TOP 10 A, B, C FROM DDDD WHERE E>'0'");
	$a = 1;
	
	foreach ($result as $record) {
		  
		  $rows[] = array($a, $record['A'], $record['B'], $record['C']);  
		  $a++;
	}

	return theme('table', array('rows' => $rows,'header' => $header,));
}
?>

Thank you all for your time and effort.
Emil

damien tournoud’s picture

Category: bug » support
Status: Active » Closed (won't fix)

Triaging. It is not in the scope of this project to support neither Linux nor SQL Server 2000.

The fatal error here is by design: it is PHP broken way to do runtime checks :)

Jean Gionet’s picture

@emilacosta
where exactly did you put this custom code? I'd like to test it out with this old MSSQL 2000 server I have collecting dust!
thanks

emilorol’s picture

@Jean Gionet,
I created a module where I inserted the code to display the data as a regular Drupal table. I also did for someone else reading this threat the other day a modification for a cron job using Drupal 7 to import data from MSSQL into Drupal as nodes. Here is the new code:

<?php

/**
  *  Implements hook_cron
  */
  
  function YOUR_MODULE_cron() {
  
    //  Get last process unique ID
	$last_id = variable_get('mymodule_last_id', 0);
  
    //  Stablish a connection to the remote MSSQL server
	$conn = new PDO("dblib:host=192.168.1.100;dbname=YOUR_DB", 'YOUR_USERNAME', 'YOUR_PASSWORD');
	
	//  Prepate query to execute
	$result = $conn->query('SELECT TOP 10 A, B, C FROM DDDD WHERE E>:last_id, array(':last_id' => $last_id) ');
  
    //  Loop thru the results if any
	foreach ($result as $record) {

		//  Create new node
		$node = new StdClass();
		$node->type = 'story'; // Content type
		$node->language = LANGUAGE_NONE;
		$node->uid = 1; // User ID
		node_object_prepare($node);

		$node->title = $record['A'];   // Node title

		$node->body[$node->language][0]['value']   =$record['B'];
		$node->body[$node->language][0]['summary'] = $record['C'];
		$node->body[$node->language][0]['format']  = 'filtered_html';

		//  Save new node (You might even consider destroying the node object using the unset function)
		$node = node_submit($node);
		node_save($node);
		
		//  Log new created node
        watchdog('YOUR_MODULE', 'New node created from MSSQL import %data', array('%data' => $record['A']), WATCHDOG_NOTICE, $link = NULL);

    }
  
  }
?>

Status: Active » Closed (won't fix)