What function do you use to connect to MSSQL from PHP 5.3.3?

Good morning, I've been using these http://us.php.net/manual/en/book.mssql.php but when we migrated to the newest php version I was forced to find an alternative solution. I've been told to start using SQLSRV extension so I started playing around with it but it  doesn't work for one of my queries and even doesn't return any errors.

This is the code:

 $sql = "EXEC SHOW_TICKET_VIEW_NEW1 '".$STATUS."','".$SHOW."','(".
$result  = sqlsrv_query($dbh1, $sql, array(), array( "Scrollable" =>
SQLSRV_CURSOR_KEYSET )) or die ("Could not get results1.");
if (sqlsrv_num_rows($result) === false){
        echo "ERROR";

if( ($errors = sqlsrv_errors() ) != null)
   foreach( $errors as $error)
          echo "SQLSTATE: ".$error[ 'SQLSTATE']."\n";
          echo "code: ".$error[ 'code']."\n";
          echo "message: ".$error[ 'message']."\n";


All what I  get is Could not get results1.

Please advise.
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Ray PaseurConnect With a Mentor Commented:
I'm guessing "latest version" means PHP 5.3.3?  Have a look at these links:

Nothing jumped out here:

In short, it looks like there is no need to change to SQLSRV.  Just install the MSSQL and keep running.
joein610Author Commented:
You have to install the extension to be able to use the SQLSRV commands. In my case I had to download the extension file and attached it to my php.ini file:


Downloads are available here:

I have no problems using it besides the query I provided before. No errors, nothing. Why ?
I use pdo:

Here is a connection class I use that gets its details from a ini file

Here is the ini file:

; this is an INI file
db_user=[dbusename here]

host=[put server ip/url here]
dbname=[db name here]



Open in new window

Connection class:


 * This class creates a single db connection
include_once 'dbSettings.php';

class DBC {

    private static $objInstance;
    private function __construct() {


    private function __clone() {

    public static function getInstance() {
        $settings = new dbSettings();
        $auth = $settings->getAuth();
        if (!self::$objInstance) {
            try {
                self::$objInstance = new PDO($auth[0], $auth[1], $auth[2], $settings->getOptions());
            } catch (Exception $e) {
                echo "Failed: " . $e->getMessage();
            foreach ($settings->getAttrib() as $k => $v) {
                        , constant("PDO::{$v}"));
        return self::$objInstance;

    final public static function __callStatic($chrMethod, $arrArguments) {
        $objInstance = self::getInstance();
        return call_user_func_array(array($objInstance, $chrMethod), $arrArguments);



Open in new window

The class that loads the settings:


class dbSettings {

    private $options, $auth, $attrib;

    function dbSettings() {

    public function setSettings() {
        $ini = "connectionSettings.ini";
        $parse = parse_ini_file($ini, true);
        $driver = $parse ["db_driver"];
        $dsn = "${driver}:";
        $user = $parse ["db_user"];
        $password = $parse ["db_password"];
        $options = $parse ["db_options"];
        $attributes = $parse ["db_attributes"];
        foreach ($parse ["dsn"] as $k => $v) {
            $dsn .= "${k}=${v};";
        $t = array($dsn, $user, $password);
        $this->setAuth(array($dsn, $user, $password));

    private function setOptions($_value) {
        $this->options = $_value;

    function getOptions() {
        return $this->options;

    private function setAuth($_value) {
        $this->auth = $_value;

    function getAuth() {
        return $this->auth;

    private function setAttrib($_value) {
        $this->attrib = $_value;

    function getAttrib() {
        return $this->attrib;


Open in new window

to use it/ ie to get a connection I

include the file:

include_once 'DBC.php';

get the connection:

$con = DBC::getInstance();

I then pass call to the connection:

$count = $con->exec("DELETE FROM fruit WHERE colour = 'red'");



What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Ray PaseurCommented:
Not sure why you are not getting error output.  I assume you have tested it with a deliberately induced error, right?  I am a little suspicious of this sort of thing:

if (sqlsrv_num_rows($result) === false){
        echo "ERROR";

Are you sure that the sqlsrv_num_rows() function will return FALSE  -- or maybe it will return zero, which is not the same as FALSE.
joein610Author Commented:
Are you sure that the sqlsrv_num_rows() function will return FALSE  -- or maybe it will return zero, which is not the same as FALSE.

joein610Author Commented:
Also with PDO:

"This extension is not available anymore on Windows with PHP 5.3 or later. "

Ray PaseurCommented:
Are you hooked up with this: http://blogs.msdn.com/b/sqlphp/
On Windows, you should use SqlSrv, an alternative driver for MS SQL is available from Microsoft: » http://www.microsoft.com/sqlserver/2005/en/us/PHP-Driver.aspx . The documentation is available at » http://msdn.microsoft.com/en-us/library/cc296221.aspx
joein610Author Commented:
As I said. I have the SQLSRV extension already installed, up & running but it seems like it doesn't like my stored procedures and it doesn't even return any errors. Do I have to handle stored procedures other way than using the regular php_mssql extension?
Here is a snippet of a class I have just written that uses the regular mssql connection, its being written to be used on PHP v 4.8.8 though...

include_once 'includes/dbSettings.php';
$c = new WriteOut();

class WriteOut {

    var $stmt, $link,$dbs;

    function WriteOut() {
        $this->dbs = new dbSettings();


    function connect() {

        $this->setLink(mssql_connect($this->dbs->getHost(), $this->dbs->getDbUser(), $this->dbs->getPassword()));

        (!$this->getLink()) {
            die('Could not connect: ' . mssql_error());
        'Successful connection';
//lets test calling a procedure
        $rtn = null;
        mssql_select_db($this->dbs->getDbname(), $this->getLink());
        $this->setStmt(mssql_init('Qualification.spMultiplePathwaysSource', $this->getLink()));

    function writeData() {
        $pid = 4353;
        $cstat = 2;
        mssql_bind($this->getStmt(), '@providerid', $pid, SQLINT4, false, false);
        mssql_bind($this->getStmt(), '@courseStatus', $cstat, SQLINT4, false, false);
        mssql_bind($this->getStmt(), '@ReturnMessage', $rtn, SQLVARCHAR, true);
        $test = mssql_execute($this->getStmt());
        $op = mssql_fetch_assoc($test);
        print "<pre>";
        print "</pre>";


    function setLink($cnstr) {
        $this->link = $cnstr;

    function getLink() {
        return $this->link;

    function closeLink() {
        print "got here";

    function setStmt($a_stmt) {
        $this->stmt = $a_stmt;

    function getStmt() {
        return $this->stmt;



Open in new window

it obviously depends on some other classes, but gives the idea of calling a procedure this way
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.