Jump to content

PHP Version 5.4.0 and MSSQL


dieterm

Recommended Posts

The mssql_* set of functions are not supported since 5.3.  The preferred extensions to use now are SQLSRV (on windows) and ODBC (on linux).

 

If your code uses the mssql_* functions you will either have to re-write the code to use the new drivers, downgrade back to php 5.2, or scour the web and see if you can find a third-party dll to enable the extension.

Not sure if a find and replace would work.  I use the PDO api in my application rather than the function based api.

 

You could create an include file of wrapper functions to re-define the mssql_* functions, I did something like that in my code as a quick-fix to get some third-party code working until it could be re-written.

 

For example:

if (!function_exists('mssql_query')){
   include 'mssql_compat.inc.php';
}

 

mssql_compat.inc.php - keep in mind this is using pdo as a base, your implementations would differ.

<?php

/**
* mssql_query replacement.
* 
* Provides a wrapper for mssql_query
*
* @param string $sql The query to execute.
* @param PDO $link The connection to query on.  Defaults to the currently active connection.
* @return PDOStatement
*/
function mssql_query($sql, $link=null){
if (!$link) $link=DB::GetFromPool();

$stmt = $link->prepare($sql);
if (!$stmt){
	throw new SQLException($link, $sql);
}

if (!$stmt->execute()){
	throw new DatabaseException($link, $stmt, $sql);
}

$stmt->_rows = $stmt->fetchAll();
reset($stmt->_rows);

return $stmt;
}

/**
* mssql_fetch_array replacement.
*
* Provides a wrapper for mssql_fetch_array
*
* @param PDOStatement $stmt The result set.
* @param int $type The type of results to fetch, should be one of the PDO class constants.
* @return mixed
*/
function mssql_fetch_array($stmt, $type=PDO::FETCH_BOTH){
if (!($stmt instanceof PDOStatement)){
	throw new InvalidArgumentException('$stmt must be a PDOStatement object');
}

if (is_array($stmt->_rows)){
	$row=current($stmt->_rows);
	next($stmt->_rows);

	if ($type == PDO::FETCH_BOTH || !is_array($row)){
		return $row;
	}
	else {
		$ret=array();
		foreach ($row as $k=>$v){
			if ($type == PDO::FETCH_ASSOC && !is_int($k)){
				$ret[$k] = $v;
			}
			else if ($type == PDO::FETCH_NUM && is_int($k)){
				$ret[$k] = $v;
			}
		}
		return $ret;
	}
}
else {
	return false;
}
}

///more functions

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.