hexblot/mssql-compat

Small wrapper for PDO that emulates the deprecated mssql_* functions, and allows legacy software to work in PHP7+

v0.2.2 2017-08-11 11:17 UTC

This package is not auto-updated.

Last update: 2024-05-12 02:51:19 UTC


README

MSSQL Compat is a small and simple drop-in library that allows your pre-7 PHP project, that uses MSSQL via mssql_* functions ( eg mssql_connect() et al ), to work transparently with PDO DBLib by providing the missing functions as wrappers around the equivalent PDO syntax - and all that with a single line!

The story

I recently started migrating internal web applications to new servers that use PHP7.

Thus I stumbled on a couple of old applications that use MSSQL with PHP, which I dearly hoped to avoid rewriting and debugging now that PHP7 has deprecated the mssql_* family of functions.

The result is this single-file solution: just include it before your first mssql_* call, and it will transparently wrap them to the equivalent PDO calls.

What it is not

  • This library is not a security enhancement by any measure. It assumes that your program logic escapes what needs to be escaped, since you cannot benefit from PDO goodness without changing the function syntax.
    • That being said, kindly let me know if you can provide better security in the given code.
  • It is not meant as a means to develop new software in PHP7 using mssql_* functions - use PDO or similar!

Requirements

MSSQL-Compat requires the bare basics for PHP7 to be able to talk to MSSQL - namely:

Hint: All of the above are easily usable via repos such as IUS or Remi

How to use

Option A: Manually

Just download and add an include to this library on the top of the old application index.php file.

  <?php
     include("path/to/mssql_compat.php");

     # Insert old application logic here

Option B: Via Composer

$ composer require hexblot/mssql-compat

If your app doesn't already have one, add a require towards the autoload.php file generated by composer in your index.php:

  <?php
    require __DIR__.'/path/to/vendor/autoload.php';

Under the hood: Which functions are covered

This is the list of the functions that are currently covered. They were sufficient for my needs, but, understandably, you may need more for your projects. Let me know in the issues - patches are of course very welcomed!

  • mssql_​bind
  • mssql_​close
  • mssql_​connect
  • mssql_​data_​seek
  • mssql_​execute
  • mssql_​fetch_​array
  • mssql_​fetch_​assoc
  • mssql_​fetch_​batch
  • mssql_​fetch_​field
  • mssql_​fetch_​object
  • mssql_​fetch_​row
  • mssql_​field_​length
  • mssql_​field_​name
  • mssql_​field_​seek
  • mssql_​field_​type
  • mssql_​free_​result
  • mssql_​free_​statement
  • mssql_​get_​last_​message
  • mssql_​guid_​string
  • mssql_​init
  • mssql_​min_​error_​severity NOTE: Placeholder only to avoid errors!
  • mssql_​min_​message_​severity NOTE: Placeholder only to avoid errors!
  • mssql_​next_​result
  • mssql_​num_​fields
  • mssql_​num_​rows
  • mssql_​pconnect
  • mssql_​query
  • mssql_​result
  • mssql_​rows_​affected
  • mssql_​select_​db